私のnodejs apiでmongodbに接続できますが、何らかの理由でデータが返されません。私は、コンソールからのMongoDBを照会すると、ここで私が得るものです:Mongodbのデータがnodejs APIに返されない
MongoDB Enterprise > db.patients.find();
{ "_id" : ObjectId("58c9a5dd1eb8c7c1c68778ca"), "FName" : "n", "LName" : "ri", "Email" : "[email protected]", "phone" : "1234567890" }
ここnodejsからのコードです:
app.get('/api/patients',function(req,res){
Patient.getPatients(function(err,patients){
if(err){
throw err;
}
console.log(patients.length);
res.json(patients);
});
});
と、これはgetPatients方法である:
var Patient = module.exports = mongoose.model('patient',patientSchema);
// get patients
module.exports.getPatients = function(callback,limit){
Patient.find(callback);
}
しかし、ブラウザが常に[]]とデータは表示されません。 APIのarray(console.log(patients.length))の長さを見ると、0が得られます。データが返されない理由はわかりません。 誰かが私に問題を指摘することができますか?提案に基づいて
更新
、私は変更を次のようでした。コレクションが存在することを確認するために2つのコレクションを作成しました。だから私は患者と患者のコレクションを持っています。私はこのコードを実行すると
var mongoose = require('mongoose');
var patientSchema = mongoose.Schema({
FName: {
type : String
},
LName: {
type : String
},
Email: {
type : String
},
phone: {
type : String
},
},
{collection : 'patient'});
var Patient = module.exports = mongoose.model('patient',patientSchema);
// get patients
module.exports.getPatients = function(callback,limit){
console.log('test2');
Patient.find({}, function(err, patients) {
if (err) throw err;
// object of all the users
console.log(patients.length);
});
}
が、それはtest2は 'と私のための「0」ではなく、実際の結果が出力されます。ここでは
MongoDB Enterprise > db.patient.find();
{ "_id" : ObjectId("58cfe4551eb8c7c1c68778cc"), "FName" : "V", "LName" : "K", "Email" : "[email protected]", "phone" : "1234567890" }
{ "_id" : ObjectId("58cfe4601eb8c7c1c68778cd"), "FName" : "V1", "LName" : "K1", "Email" : "[email protected]", "phone" : "1234567890" }
MongoDB Enterprise > db.patients.find();
{ "_id" : ObjectId("58cfe42d1eb8c7c1c68778cb"), "FName" : "V", "LName" : "K", "Email" : "[email protected]", "phone" : "1234567890" }
はnodejsファイルからコードです。誰かがこれを助けることができますか?
スキーマは、私のアップデートで定義され、共有されています。確認してください。 – DevHelp
コードをgithubにプッシュできますか?クローンして問題を解決できますか? –
https://github.com/justdev123/Mean - >それは大きなプロジェクトかもしれませんが、関連ファイルは1)src \ models \ patient.js 2)app.js – DevHelp