MissingSchemaError: Schema has not been registered for model "Product"

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 the objects will have, until everything is ok. Following the video in question, I happen to the same error occurred at 5:39 , however the person of the video arranged by adding the following line in his code: "const Product = require('./models/product');" However, when I do the same, I still have the same error in question, where I can not identify what is wrong, even after trying to re-write this piece of code. For more 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);
};
    
asked by anonymous 06.12.2018 / 23:12

1 answer

1
  

app.js

No need to instantiate 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 I instantiate the schema.

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

Changed how the model instance.

//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);
};
    
07.12.2018 / 00:47