0
I’m developing a simple script, but I’m stuck in an error that I can’t understand the reason, I created a model and apparently the error occurs whenever I try to access.
Node:
router.get('/postagens', (req, res) => {
Postagem.find().populate("categorias").sort({data: "desc"}).then((postagens) => {
res.render("admin/postagens", {postagens: postagens})
}).catch((err) => {
req.flash("error_msg", "ouve um erro ao listar postagens")
res.redirect("/admin")
})
})
Mongoose:
const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const Postagem = new Schema({
titulo:{
type: String,
required: true
},
slug:{
type: String,
required: true
},
descricao:{
type: String,
required: true
},
conteudo:{
type: String,
required: true
},
categoria:{
type: Schema.Types.ObjectId,
ref: "Categorias",
required: true
},
data:{
type: Date,
default: Date.now()
}
})
mongoose.model("postagens", Postagem)
The error that is shown is as follows:
Referenceerror: Postagem is not defined at router.get (/home/ericocalasans/Documents/nodejs/filanave/ericoEstudos/blogapp/Routes/admin.js:107:5) at Layer.Handle [as handle_request] (/home/ericocalasans/Documents/nodejs/filanave/node_modules/express/lib/router/layer.js:95:5) at next (/home/ericocalasans/Documents/nodejs/filanave/node_modules/express/lib/router/route.js:137:13) At route.Dispatch (/home/ericocalasans/Documents/nodejs/filanave/node_modules/express/lib/router/route.js:112:3) at Layer.Handle [as handle_request] (/home/ericocalasans/Documents/nodejs/filanave/node_modules/express/lib/router/layer.js:95:5) at /home/ericocalasans/Documents/nodejs/filanave/node_modules/express/lib/router/index.js:281:22 At function.process_params (/home/ericocalasans/Documents/nodejs/filanave/node_modules/express/lib/router/index.js:335:12) at next (/home/ericocalasans/Documents/nodejs/filanave/node_modules/express/lib/router/index.js:275:10) At function.Handle (/home/ericocalasans/Documents/nodejs/filanave/node_modules/express/lib/router/index.js:174:3) at router (/home/ericocalasans/Documents/nodejs/filanave/node_modules/express/lib/router/index.js:47:12) at Layer.Handle [as handle_request] (/home/ericocalasans/Documents/nodejs/filanave/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/home/ericocalasans/Documents/nodejs/filanave/node_modules/express/lib/router/index.js:317:13) at /home/ericocalasans/Documents/nodejs/filanave/node_modules/express/lib/router/index.js:284:7 At function.process_params (/home/ericocalasans/Documents/nodejs/filanave/node_modules/express/lib/router/index.js:335:12) at next (/home/ericocalasans/Documents/nodejs/filanave/node_modules/express/lib/router/index.js:275:10) at app.use (/home/ericocalasans/Documents/nodejs/filanave/ericoEstudos/blogapp/app.js:49:13)
The user reported an error that cannot understand the reason. The code solves the error but does not answer the question. Explain what it means
module.exports
the why to use, how to use and what is the difference to themodule.default
. Add value to your answer.– Augusto Vasques