0

I'm in process of an app based on Node.js, MongoDB and Express. My goal is to get a fetch API system to work.

When using Postman to check out my status, the GET for "article.js" model file (in my localhost:3000/articles) shows the following error:

{
    "error": {
        "message": "Schema hasn't been registered for model \"Category\".\nUse mongoose.model(name, schema)",
        "name": "MissingSchemaError"
    }
}

This error disables the display of my articles or categories in Postman, as they are saved in my MongoDB project area at mongodb cloud. The model file code "article.js" is the following:

const mongoose = require('mongoose');

const articleSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    title: { type: String, required: true },
    description: { type: String, required: true },
    content: { type: String, required: true },
    categoryId: { type: mongoose.Schema.Types.ObjectId, required: true, ref: 'Category' }
});

module.exports = mongoose.model('Article', articleSchema);

This file connects with the controller named "articles.js", with the following relevant code:

const mongoose = require('mongoose');
const Article = require('../models/article');
const Category = require('../models/category');

module.exports = {
    getAllArticles: (req, res) => {
        Article.find().populate('categoryId', 'title').then((articles) => {    
            res.status(200).json({
                articles
            })
        }).catch(error => {
            res.status(500).json({
                error
            })        
        });
    },
    createArticle: (req, res) => {

    const { title, description, content, categoryId } = req.body;

    Category.findById(categoryId).then((category) => {
        if (!category) {
            return res.status(404).json({
                message: 'Category not found'
            })
        }

        const article = new Article({
            _id: new mongoose.Types.ObjectId(),
            title,
            description,
            content,
            categoryId
        });     

        return article.save();
    }).then(() => {
        res.status(200).json({
            message: 'Created article'
        })
    }).catch(error => {
        res.status(500).json({
            error
        })        
    });    
},
}

The model file "category.js" code in the app looks like this:

const mongoose = require('mongoose');

const categorySchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    title: { type: String, required: true },
    description: { type: String, required: true }
});

module.exports = mongoose.model('Category', categorySchema);

I looked up topics from the past here, such as this one - but it didn't solve my problem.

What should I do in order to fix my code?

Is it a syntax mistake or something else?

Yair Shachar
  • 55
  • 1
  • 11
  • Where is your code showing your Category model? You are showing the import but cannot see the code for when you are creating that model – SenAnan Feb 24 '20 at 11:21
  • See I added some relevant code, tell me if you understand now what's the solution. – Yair Shachar Feb 24 '20 at 12:13
  • Why are you trying to populate the 'title' field - that field is not a referenced field but is native to the article model. Try removing that – SenAnan Feb 25 '20 at 10:48
  • Well, I continued writing some code and now it works fine. Probably you are right, thare was no special problem in thte code itself. Anyway now it's ok. – Yair Shachar Feb 27 '20 at 13:09

1 Answers1

0

the code seems to be OK

I don't see here any particular error's

ariel
  • 1
  • 1