I want to get my data from Mongo DB with Node JS and Express JS. I Already made a mongoose schema and exported the module. But when I'm running the function, I just get an empty JSON object.. here is some code:
This is my exports function:
var Buchung = module.exports = mongoose.model('Buchung', buchungSchema);
//Get Buchungen
module.exports.getBuchungen = function (callback) {
Buchung.find(callback);
};
And here's my function to actually get the data:
Buchung = require('../DB/models/buchung_mitarbeiter');
router.get('/buchungen', function(req,res){
Buchung.getBuchungen(function (err, buchung) {
if(err){
throw err;
}
console.log(buchung);
res.json(buchung);
});
});
and finally my Schema:
var buchungSchema = mongoose.Schema({
user:{
type: String,
required: true
},
tor:{
type: Number,
required: true
},
datum:{
type: String,
required: true
},
sendungsstruktur:{
type: String,
required: true
}
});
I'm greatful for any help!
I have updated the code below. please try and let me know if still facing the issue. I have marked with * so remove * before using.
Buchung = require('../DB/models/buchung_mitarbeiter');
router.get('/buchungen', function(req,res){
Buchung.getBuchungen(function (err, buchung) {
if(err){
throw err;
}
console.log(buchung);
res.json(buchung);
**res.send(buchung)**
});
});
Export your Schema.
module.exports = mongoose.model('Buchung', buchungSchema);
Buchung = require('../DB/models/buchung_mitarbeiter');
router.get('/buchungen', (req,res) => {
Buchung.find().then(buchungs => {
res.json(buchungs);
}).catch(err => res.json({err}))
});
`
var Buchung = module.exports = mongoose.model('Buchung', buchungSchema);
module.exports.getBuchungen = function (callback) {
Buchung.find()
.then(response => {
return response;
})
.catch(error => {
return error;
});
};
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.