Missingschemaerror: Schema hasn’t been Registered for model "Product"

Asked

Viewed 1,875 times

0

Well, I’m following a tutorial on Node.js and Mongodb and I’m trying to create a product, so I create a list of attributes that objects will have, until everything ok. Following the video in question, happens to me the same error occurred in 5:39, however the person in the video tidied up by adding the following line in his code: "const Product = require('./models/product');" However, when I do the same, I still have the same mistake in question, where I cannot identify what is wrong, even after trying to rewrite this piece of code. For further clarification here is my code

app js.

 const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const app = express();
const router = express.Router();

const indexRoute = require('./routes/index-route');
const productsRoute = require('./routes/products-route');


mongoose.connect('mongodb://login:[email protected]:58548/ndstr');


const Product = require('./models/product');

my product.js

'use strict';

const mongoose = require('mongoose');
const Schema = moongose.Schema;

const schema = new Schema({
    title: {
        type: String,
        required: true,
        trim: true
    },
    slug:{
        type: String, 
        required: true,
        trim: true,
        index: true,
        unique: true
    },
    description: {
        type: String,
        required: true,
    },
    price: {
        type: Number,
        required: true
    },
    active: {
        type: Boolean,
        required: true,
        default: true
    },
    tags: [{
        type: String,
        required: true
    }]
});

module.exports = mongoose.model('Product', schema);

product-controller.js

'use strict';

const mongoose = require('mongoose');
const Product = mongoose.model('Product');

exports.post = (req, res, next) => {
    var product = new Product(req.body);
    product.save().then(x => {
        res.status(201).send({ mesage: "Produto cadastrado com sucesso!'" });
    }).catch(e => {
        res.status(400).send({
            message: '[ERRO] Falha durante cadastro!',
            data: e
        });
    });
    res.status(201).send(req.body);
};
exports.put = (req, res, next) => {
    const id = req.params.id;
    res.status(200).send({
        id: id,
        item: req.body
    });
};
exports.delete = (req, res, next) => {
    res.status(201).send(req.body);
};
  • Take a look at these tutorials from Rocketseat are many good besides giving good practice tips.

1 answer

2


app js.

Not the need to instill the product in the app being already done in the controller, in the app is only opening the connection.

Required if version of Mongoose is greater than 5.3.10 useNewUrlParser: true useCreateIndex: true

const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));

const indexRoute = require('./routes/index-route');
const productsRoute = require('./routes/products-route');

mongoose.connect('mongodb://login:[email protected]:58548/ndstr',
    { 
        useNewUrlParser: true , 
        useCreateIndex: true
    });

//const Product = require('./models/product');

app.use('/produtos', productsRoute);

product js.

I changed the way the schema instantiates.

const mongoose = require('mongoose');
//const Schema = moongose.Schema;
//const schema = new mongoose.Schema({
const productSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true,
        trim: true
    },
    slug:{
        type: String, 
        required: true,
        trim: true,
        index: true,
        unique: true
    },
    description: {
        type: String,
        required: true,
    },
    price: {
        type: Number,
        required: true
    },
    active: {
        type: Boolean,
        required: true,
        default: true
    },
    tags: [{
        type: String,
        required: true
    }]
});

module.exports = mongoose.model('Product', productSchema );

controller js.

I changed the way the instance model.

//const mongoose = require('mongoose');
//const Product = mongoose.model('Product');
const Product = require('../models/Product')

exports.post = (req, res, next) => {
    var product = new Product(req.body);
    product.save().then(x => {
        res.status(201).send({ mesage: "Produto cadastrado com sucesso!'" });
    }).catch(e => {
        res.status(400).send({
            message: '[ERRO] Falha durante cadastro!',
            data: e
        });
    });
    res.status(201).send(req.body);
};
exports.put = (req, res, next) => {
    const id = req.params.id;
    res.status(200).send({
        id: id,
        item: req.body
    });
};
exports.delete = (req, res, next) => {
    res.status(201).send(req.body);
};
  • Perfect bro! Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.