Answer the question
In order to leave comments, you need to log in
Why does Mongoose find() return an empty array?
Hello! I recently started studying the Node.js + MongoDB (Mongoose) stack and ran into a problem, the find method always returns an empty array, although there is data in the collection
const mongoose = require("mongoose");
const schemas = require('./dbSchemas');
const enviroments = require('./enviroment');
module.exports = {
getMenu(res) {
//dbUrl = "mongodb://localhost:27017/Pizza"
mongoose.connect(enviroments.dbUrl, { useNewUrlParser: true, useUnifiedTopology: true });
const Menu = mongoose.model('menu', schemas.menuSchema);
Menu.find({}).exec() // callback также, не работает
.then(menu => {
console.log(menu) // здесь выводит пустой массив
res.send(menu);
})
.catch(err => {
res.send(err);
})
.finally(() => {
mongoose.disconnect();
})
}
}
menuSchema: new Schema({
pizzaName: {
type: String,
required: true
},
ingridients: {
type: [String],
required: true
},
price: {
type: Number,
required: true,
}
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question