简体   繁体   中英

TypeError: elements.find is not a function NodeJS + Mongoose

I am trying this code but I get this error: "TypeError: elements.find is not a function"

Can you help me?

This is my model:

const mongoose = require('mongoose');

const elementSchema = new mongoose.Schema({
    nombre: {
        type: String,
        required: true
    },
    comunidad: {
        type: String,
        required: true
    },
    fabricante: {
        type: String,
        required: true
    },
    fecharegistro: {
        type: Date,
        defaault: Date.now
    }

});

module.exports = mongoose.model('Element', elementSchema);

I put this in my routes.js:

const elements = require('./models/elements')();

app.get('/network-elements', isLoggedIn, (req, res) => {
        elements.find({}, (err, element) =>{
            if (err) throw err;
            res.render('network-elements', {
                user: req.user,
                nombre: elements.nombre
            });
        });

    });

Then when I try to see my networ-elements.ejs I get the error

Your import is the point about that:

const elements = require('../models/elements')(); // change this
const elements = require('../models/elements'); // for this

Actually you do need this "()" in your module import!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM