2017-02-21 4 views
0

をNodejs私はこのようなデータを取得していますマングースデータにアクセスする方法:これはコードです</p> <p>:

User.find({ Username: user }, function(err, found_user) { 

        console.log('user data'+ found_user); 

        if(found_user.length > 0){ 

         console.log('inside found user'); 
         var recordings = found_user.recordings; 
         console.log(recordings) 
         for (var singleRecords in recordings){ 
          console.log("Single record :"+singleRecords); 
          if(!singleRecords.isPlayed){ 
           console.log(singleRecords.playingUrl); 
           twiml.play(singleRecords.playingUrl); 
           found_user.recordings[singleRecords].isPlayed = true; 
           found_user.save(function (err) { 
            if(err) 
             throw err 
           }); 


          } 
         } 
        } 

そして、これが見つかりました。ユーザーの値である:

user data { Username: 'B', 
    __v: 2, 
    _id: 58ac15e4b4e1232f6f118ba3, 
    recordings: 
     [ { isPlayed: false, 
      playingUrl: 'http://localhost:8000/public/toplay/playing_file_1487672817599.mp3' }, 
     { isPlayed: false, 
       playingUrl: 'http://localhost:8000/public/toplay/playing_file_1487672827411.mp3' } ] 
     } 
inside found user 

で変数found_user。しかし、それは私にそれの中の任意のデータを与えていません。 found_user.Usernameのように未定義の値が返されます。 私はそのレコーディングアレイを変数の中に保存したいと思います。どのようにそれを行うにはどのようなアイデア?

+1

私たちにあなたがしようとしているコードを表示してください? –

+1

最初のログfound_userは 'console.log(found_user);'を使用して出力を表示します – nivas

+0

@nivas更新された質問を参照してください –

答えて

1

find()は、ドキュメントではない配列を期待していますように、ライン

var recordings = found_user.recordings; 

が動作しません、したがって、コールバックで条件に一致するドキュメントの配列を返します。あなたのようにドキュメントを返しfindOne()メソッドを使用することができます

User.findOne({ Username: user }.exec(function(err, found_user) {  
    console.log('user data'+ found_user); 
    if (found_user) { 
     console.log('inside found user'); 
     var recordings = found_user.recordings; 
     console.log(recordings); 
    } 
}); 
関連する問題

 関連する問題