我试图使用mongoose从MongoDB数据库中获取数据并在Node js中表达,我创建了Model并成功连接到数据库,但是问题是当数据库集合中已经有文档时,我得到的响应是空的。
模型类:
const mongoose = require("mongoose");
const RendezvousSchema = mongoose.Schema({
idpatient: {
type: String,
required: true
},
idmedecin: {
type: String,
required: true
},
});
module.exports = mongoose.model("Rendezvous", RendezvousSchema);
index.js:
const express = require("express");
const router = express.Router();
const Rendezvous = require("../model/RendezVous");
/* Get Rendez Vous By id */
/**
* @method - GET
* @description - Get Rendez vous Medecin
* @param - /user/GetRdvMedecin
*/
router.get("/GetRdvMedecin", async (req, res) => {
try {
const rdv = await Rendezvous.find();
res.json(rdv);
console.log('Fetched');
} catch (e) {
res.send({ message: "Error in Fetching rendez vous" });
}
});
/* Get Rendez Vous By id*/
module.exports = router;
这是console.log:
Server Started at PORT 4000
Connected to DB !!
Fetched
邮递员GET请求:“ http://localhost:4000/user/GetRdvMedecin”
邮递员回复:'[]'
文档样本:
{
"_id":{
"$oid":"5e7396787b32a12e38a7aa7d"
},
"idpatient":"5e6ce11bc31de6132ca454a1",
"idmedecin":"5e5aa519190d8c2818a66a0a"
}
提示:当我使用另一个模型(“用户”)时,它工作正常并返回响应。
答案 0 :(得分:0)
您的请求.find()可以返回多个对象,我总是返回如下内容:
input.next()
答案 1 :(得分:0)
通过在模型中添加集合名称来解决
const mongoose = require("mongoose");
const RendezvousSchema = mongoose.Schema({
idpatient: {
type: String
},
idmedecin: {
type: String
}
},{
collection: 'Rendezvous'
});
// export model user with UserSchema
module.exports = mongoose.model("Rendezvous", RendezvousSchema);