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.
– Chance