2017-03-17 5 views
1

私の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ファイルからコードです。誰かがこれを助けることができますか?

答えて

0

はあなたもあなたのスキーマを定義し、その非常によく、エクスポートあなたは確かにあります...はい、あなたのnodejsスクリプトから

//get the model 
var Patient = mongoose.model('patient',patientSchema); 
// get all the patients 
Patient.find({}, function(err, patients) { 
    if (err) throw err; 

    // object of all the users 
    console.log(patients); 
}); 

をこのような何かを、私は、これは上記

+0

スキーマは、私のアップデートで定義され、共有されています。確認してください。 – DevHelp

+0

コードをgithubにプッシュできますか?クローンして問題を解決できますか? –

+0

https://github.com/justdev123/Mean - >それは大きなプロジェクトかもしれませんが、関連ファイルは1)src \ models \ patient.js 2)app.js – DevHelp

0

答えを動作するはずと信じていた場合、患者のモデルの検索を正しく実行しても、結果をあなたのAPIの応答に戻すことはできません。私は、あなたがgetPatientsコードをこれに変更すると思います:

var Patient = module.exports = mongoose.model('patient',patientSchema); 

module.exports.getPatients = function(callback) { 
    Patient.find({}, 
     function(err, data) { 
      callback(err,data); 
     } 
    ); 
} 

これは動作します。それがうまくいかない場合、私はapp.getがgetPatients関数にアクセスできることを確認するためにコードファイル全体を見たいでしょうが、そうでない場合はエラーが発生します。だから、これはうまくいくと思います。

0

私のコードは正しいと思われましたが、接続文字列が正しくありませんでした。

誤った接続文字列:

mongoose.connect( 'のMongoDB:// localhostを:27017 /');

正しい接続文字列:

mongoose.connect( 'のMongoDB:// localhostを:27017/患者');

関連する問題