[英]Node JS and Express cannot read data from Mongo DB
I want to get my data from Mongo DB with Node JS and Express JS.我想使用 Node JS 和 Express JS 从 Mongo DB 获取我的数据。 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:
但是当我运行这个函数时,我只是得到一个空的 JSON 对象..这是一些代码:
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;
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.