2016-11-21 10 views
0

私のデータベースにjsonオブジェクトを保存しようとしています。 save()関数は呼び出されていませんが、jsonオブジェクトは保存されません。 問題を解明するのに役立ちます。 私はそれがマングースとの接続の問題だと思います。 ここに私のコードは..ですmongooseスキーマオブジェクトでsave()コールバックが呼び出されない

var config = require('../config'); 
    var user = require('../user'); 
api.post('/addUser',function(req,res) { 
    var userID; 
    //creating a sample user under Model collection User.. so this becomes a document!! 
    console.log("addition of new user api hit!!"); 
    //sending a query to retrieve the no of users served 
    MongoClient.connect(dbURL, function (err, db) { 
     var UserCountCursor = db.collection("ourusers").find({"docName": "userCount"}).limit(1); 

     UserCountCursor.each(function (err, doc) { 
      if (err) 
       console.log("did not get the count"); 
      else 
      // var countString= JSON.stringify(doc); 
      //var docJson=JSON.parse(countString); 
       console.log("the json content is:" + doc.iparkoUserCount); 

      //increase the user count by 1 in the db. 
      var incCount = parseInt(doc.iparkoUserCount) + 1; 
      console.log("no of userrs:" + incCount); 
      // making an userId 
      userID = "ipkoID_C" + incCount.toString(); 
      //updating using MOngoClient 
      db.collection("ourusers").update({"docName": "userCount"}, {$set: {"iparkoUserCount": incCount}}); 
      console.log("the user count in the db has been updated!!"); 
      console.log("generated id for this guy is:" + userID); 

      if (userID != null) { 
       console.log("calling the save function"); 
       //closing the mongoclient connection 
       db.close(); 
       signUpUser(userID); 
      } 
     }); 
    }); 


    function signUpUser(userIDD) { 
     var me = new user({ 
      name: req.body.new_name, 
      password: req.body.new_pswd, 
      username: req.body.new_username, 
      phno: req.body.new_phn, 
      userId: userIDD 
     }); 

     console.log("the obj ::" + JSON.stringify(me)); 
     console.log("obj created and ready to be stored"); 
//connecting to the db using mongoose 
     mongoose.connect(config.database, function (err) { 
      if (err) 
       console.log("The error is :"+err); 
      else { 
       console.log("WE ARE CONNECTED USING MONGOOSE"); 
       //saving the sample user document 
       me.save(function (err) { 
        console.log("in the save func"); 
        if (err) throw err; 
        else { 
         console.log('User saved Successfully!!!!!'); 
         res.json({ 
          'whatStatus': 'user saved in the database!!', 
          'userID': userIDD 
         }); 
         mongoose.connection.close(); 
        } 
       }); 
      } 
     }); 
    } 
}); 

マイコンソールログ::

addition of new user api hit!! the json content is:143 no of userrs:144 the user count in the db has been updated!! generated id for this guy is:ipkoID_C144 calling the save function the obj ::{"name":"Abhi","password":"jio","username":"abhijio","phno":"45142545","userId":"ipkoID_C144","_id":"583295bfa0f9f8342035d3b9"} obj created and ready to be stored C:\Users\shivendra\WebstormProjects\iParko\node_modules\mongodb\lib\utils.js:98 process.nextTick(function() { throw err; }); ^

はTypeError:\ Users \ユーザーshivendra \ WebstormProjects \ iParko \ルート:Cでヌル のプロパティ 'iparkoUserCount' を読み取ることができません\ Users \ shivendra \ WebstormProjects \ iParko \ node_modules \ mongodb \ lib \ utils.js:96:12) C:\ Users \ shivendra \ WebstormProjects \ iParko \ RegisteredParkingLots.js:76:57 handleCallback(C: handleCallback(C:\ Users \ shivendra \ W)のnode_modules \ mongodb \ lib \ cursor.js:742:16 handleCallback(C:\ Users \ shivendra \ WebstormProjects \ iParko \ node_modules \ mongodb \ lib \ cursor.js:676:5 )の をC:\ Users \ shivendra \ WebstormProjects \ iParko \ node_modules \ mongodb \ lib \ utils.js:96: (C:\ Users \ shivendra \ WebstormProjects \ iParko \ node_modules \ mongodb \ node_modules \ 156)の に設定してください。 (C:¥Users¥shivendra¥WebstormProjects¥iParko¥node_modules¥mongodb¥node_modules¥mongodb-core¥lib¥cursor.js:588:12) で次のように入力します。次のオブジェクト(C:\ Users \ shivendra)の にあるCursor.next [asnext](C:¥Users¥shivendra¥WebstormProjects¥iParko¥node_modules¥mongodb¥node_modules¥mongodb-core¥lib¥cursor.js:681:3) \ WebstormProjects \ iParko \ node_modules \ mongodb \ lib \ cursor.js:673:8)C:\ Users \ shivendra \ WebstormProjects \ iParko \ node_modules \ mongodb \ lib \ cursor.js:262:12) handleCallback(C:\ Users \ shivendra \ WebstormProjects \ iParko)のC:\ Users \ shivendra \ WebstormProjects \ iParko \ node_modules \ mongodb \ lib \ cursor.js:746:7 の C:¥Users¥shivendra¥WebstormProjects¥iParko¥node_modules¥mongodb¥lib¥cursor.js:676:5 handleCallback(C:\ Users \ shivendra \ WebstormProjects \ iParko \ node_modules \ MongoDBの\ node_modules \ MongoDBのコア\ LIB \ cursor.js:156:5)

プロセス終了コードで仕上げ1

答えて

0

mongoose.connectでdb接続を2回開き、mongoose.connection.open()でもう1つ接続しているようです。だからこそあなたは誤りを抱えている。

以下のように1つの接続でこれを試してみてください。あなたのUserCountCursor.each(...)ループ内

mongoose.connect(config.database, function(err, db) { 
      //var dbcon=mongoose.connection.open(); 

      //dbcon.on('error',function(){console.log('connction error:')}); 
      //dbcon.once('open',function(){ 
    if(err) { 
     console.log(err); 
    } else { 
       console.log("WE ARE CONNECTED USING MONGOOSE"); 
       //saving the sample user document 
       me.save(function (err) { 
        console.log("in the save func"); 
        if (err) throw err; 
        else { 
         console.log('User saved Successfully!!!!!'); 
         res.json({ 
          'whatStatus': 'user saved in the database!!', 
          'userID': userIDD 
         }); 
         //mongoose.connection.close(); 
        } 
       }); 
      } 
    }); 
+0

Aruna私はこれを試しましたが、まだsave()関数に入っていません。私は質問とコンソールのログで私のコードを更新しました..見てみたいかもしれません.. - 助けをありがとう –

+0

@Shiven_codeboyエラーがありますか? – Aruna

+0

エラーは '' null 'のプロパティ' iparkoUserCount 'を読み取ることができません。このエラーを確認できますか? – Aruna

0

errをチェックした後、あなたはまた、docをチェックする必要があります。だから、これを持っている場所:

UserCountCursor.each(function (err, doc) { 
    if (err) 
    console.log("did not get the count"); 
    else 

    // var countString= JSON.stringify(doc); 

    //... 

}) 

ではなく、次の操作を行います。

UserCountCursor.each(function (err, doc) { 
    if (err){ 
    console.log("did not get the count"); 
    }else if(doc){ 

    // var countString= JSON.stringify(doc); 

    //... 

    } 

}) 

は、その後、あなたは Cannot read property 'iparkoUserCount' of nullエラーを回避し、あなたはあなたの save()関数に取得します。

+0

Thanx Stephen !!それは本当に働いた..それは簡単だった。 –

関連する問題