Mongoose error or nodejs

Asked

Viewed 181 times

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)

2 answers

0

You created Schema but did not export that instance. Basically, yours schema needs to be "listened to" wherever he is called, for that, you need to exportar this instance. To do that export, if you use the module.exports. In this case of yours, so we can use the Postagem, you need to put in your esquema

module.exports = mongoose.model("postagens", Postagem)

That way you can accomplish one require of this module and use the methods of mongoose belonging to this scheme.

Remember that these implementation become much easier with ES6. Since you can export yours model through

export default mongoose.model("postagens", Postagem)

And where would you do the require, you would put

import Postagem from '../meu_arquivo.js'

Has enough content on ES6 on the web, give a good research and study. This will help you a lot in your development, because, if we are going to give a general features of ES6 would be too long the answer and would lose the focus of your doubt.

In short, do you need to export a module (function)? You should use module.exports. Ah, if you have more than one function to export, you can simply do

module.exports = {
   function funcao1() {}
   function funcao2() {}
}

or

function funcao1() {}
function funcao2() {}
module.exports = {
  funcao1,
  funcao2
}

0

in the end put like this :

const postagens = mongoose.model("postagens", Postagem);

module.exports = postagens;
  • 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 the module.default. Add value to your answer.

Browser other questions tagged

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