2016-09-02 6 views
4

私は行を追加し、 "{アップサート:真}" はない:TypeError例外:callback.apply関数(Node.jsの&MongoDBの)

例外TypeError:callback.apply関数ではない、私はこのエラーを得ました

// on routes that end in /users/competitorAnalysisTextData 
// ---------------------------------------------------- 
router.route('/users/competitorAnalysisTextData/:userName') 

    // update the user info (accessed at PUT http://localhost:8080/api/users/competitorAnalysisTextData) 
    .post(function(req, res) { 

     // use our user model to find the user we want 
     User.findOne({ userName: req.params.userName}, function(err, user) { 

      if (err) 
       res.send(err); 

      console.log('user.competitorAnalysis.firstObservation: %@', user.competitorAnalysis.firstObservation); 
      // Got the user name 
      var userName = user.userName; 
      // update the text data 
      console.log('Baobao is here!'); 
      user.update(
       { 
        userName: userName 
       }, 
       { $set: { "competitorAnalysis.firstObservation" : req.body.firstObservation, 
          "competitorAnalysis.secondObservation" : req.body.secondObservation, 
          "competitorAnalysis.thirdObservation" : req.body.thirdObservation, 
          "competitorAnalysis.brandName" : req.body.brandName, 
          "competitorAnalysis.productCategory" : req.body.productCategory 
       } }, 
       { upsert: true } 
      ); 

      // save the user 
      user.save(function(err) { 
       if (err) 
        return res.send(err); 

       return res.json({ message: 'User updated!' }); 
      }); 

     }); 
    }) 

この行がない場合、エラーはありません。私はnodejsを初めて使っていますが、どこに問題があるのか​​はあまりよく分かりません。

更新

今は、エラー・メッセージが、データベースのこの部分は、新しいデータで更新されません。埋め込みドキュメントはまだ空です。

// on routes that end in /users/competitorAnalysisTextData 
// ---------------------------------------------------- 
router.route('/users/competitorAnalysisTextData/:userName') 

// update the user info (accessed at PUT http://localhost:8080/api/users/competitorAnalysisTextData) 
.post(function(req, res) { 

    console.log('1'); 

    // Just give instruction to mongodb to find document, change it; 
    // then finally after mongodb is done, return the result/error as callback. 
    User.findOneAndUpdate(
     { userName : req.params.userName}, 
     { 
      $set: 
      { "competitorAnalysis.firstObservation" : req.body.firstObservation, 
       "competitorAnalysis.secondObservation" : req.body.secondObservation, 
       "competitorAnalysis.thirdObservation" : req.body.thirdObservation, 
       "competitorAnalysis.brandName" : req.body.brandName, 
       "competitorAnalysis.productCategory" : req.body.productCategory 
      } 
     }, 
     { upsert: true }, 
     function(err, user) { 
      // after mongodb is done updating, you are receiving the updated file as callback 
      console.log('2'); 
      // now you can send the error or updated file to client 
      if (err) 
       return res.send(err); 

      return res.json({ message: 'User updated!' }); 
     }); 

}) 

答えて

8

にMongoDBのドキュメントを更新するには2通りの方法があります。

  1. それを変更し、サーバーにそれを持って、ドキュメントを見つけるには、それをmongodbに戻してください。

  2. ただドキュメントを見つけるためにmongodbに指示してください。最後にmongodbが終了した後、結果/エラーをコールバックとして返します。

コードでは、両方の方法を混在させています。 user.saveと


  1. ()、最初にあなたがuser.findOneでデータベースを検索し、サーバー(nodejs)にそれを引っ張って、今それはあなたのコンピュータのメモリに住んでいます。 その後、手動でデータを変更し、最終的に(user.saveとMongoDBのためにそれを保存することができます)

    User.findOne({ userName: req.params.userName}, function(err, user) { 
    
        if (err) 
         res.send(err); 
    
        //this user now lives in your memory, you can manually edit it 
        user.username = "somename"; 
        user.competitorAnalysis.firstObservation = "somethingelse"; 
    
        // after you finish editing, you can save it to database or send it to client 
        user.save(function(err) { 
         if (err) 
          return res.send(err); 
    
         return res.json({ message: 'User updated!' }); 
        }); 
    
  2. もう一つは代わりuser.findOneの、これが好ましいUser.findOneAndUpdate()..を使用することです()その後、user.update();それらは基本的にデータベースを2回検索しているからです。とにかくfindOne()、および(更新するために、再度検索)

に第一、第二の方法は、そのアクションでのMongoDB完了したら、あなたは意志、最初のサーバに持ち込むことなく、次のデータを更新するためにMongoDBを語っています更新されたファイル(またはエラー)をコールバックとして受け取る

User.findOneAndUpdate({ userName: req.params.userName}, 
      { 
      $set: { "competitorAnalysis.firstObservation" : req.body.firstObservation, 
         "competitorAnalysis.secondObservation" : req.body.secondObservation, 
         "competitorAnalysis.thirdObservation" : req.body.thirdObservation, 
         "competitorAnalysis.brandName" : req.body.brandName, 
         "competitorAnalysis.productCategory" : req.body.productCategory 
      } }, 
      { upsert: true }, 
     function(err, user) { 
     //after mongodb is done updating, you are receiving the updated file as callback  

     // now you can send the error or updated file to client 
     if (err) 
      res.send(err); 

     return res.json({ message: 'User updated!' }); 
     }); 
+0

私はあなたが非常にうまく説明し、2番目に示唆された方法に従うことを試みると思う。しかし、私はまだ同じエラーメッセージが表示されます。あなたはもう少し私を助けて、ポストの私の**アップデート**セクションを見てみてください。 –

+0

私はこれがうまくいくことを願っています。実際には5つの引数を渡しました。findOneAndUpdate()は4つの引数を受け入れます。 4番目はコールバックです。http://mongoosejs.com/docs/api.html#query_Query-findOneAndUpdate – vdj4y

+0

はい、非常に役に立ちます。 –

2

あなたはupdate方法

user.update(     
        { $set: { "competitorAnalysis.firstObservation" : req.body.firstObservation, 
           "competitorAnalysis.secondObservation" : req.body.secondObservation, 
           "competitorAnalysis.thirdObservation" : req.body.thirdObservation, 
           "competitorAnalysis.brandName" : req.body.brandName, 
           "competitorAnalysis.productCategory" : req.body.productCategory 
        } }, 
        { upsert: true }, 
function(err, result){} 
       ); 

update方法は3つの引数を期待するコールバックを渡すのを忘れていました。

  • 文書更新
  • オプション
  • コールバック
+0

コールバックを追加した後も同じエラーが表示されます。私の投稿の**アップデート**セクションをご覧ください。 –

+0

@ChenyaZhangその関数に余分な引数を渡します。それをチェックしてください –

+0

はい、そうです。 –

関連する問題