Thursday, 18 May 2017

Node.js MongoDB insert error

function uploadVote(voter, vote){
MongoClient.connect(url, function(err, db){
    if (err) throw err;

    db.collection('testvote', function(err, collection) {
        collection.find({"psid": voter}).toArray(function(err, result) {
            if(!result[0]){
                db.collection("testvote").insert({psid: "a", choice:0}, function(err, res) {
                    if (err) throw err;

                    console.log('insertone')
                    sendTextMessage(". ")
                });
            } else if(!result[0].choice){
                db.collection("testvote").update({psid: voter}, {$set: {'choice': vote}}, function(err, res){
                    if (err) throw err; 
                    sendTextMessage(". ")
                })
            } else{
                sendTextMessage(voter, "!");
            }

        });
        db.close(); 
    });
})
}

Hi, I'm developing a facebook messenger that let users vote something. In the code above, I see no syntax error at all. But when this function is executed it keeps making a error:

/app/node_modules/mongodb/lib/utils.js:123
2017-05-18T12:09:19.856188+00:00 app[web.1]:     process.nextTick(function() { throw err; });
2017-05-18T12:09:19.856188+00:00 app[web.1]:                                   ^
2017-05-18T12:09:19.858765+00:00 app[web.1]:     at ReplSet.insert (/app/node_modules/mongodb/lib/replset.js:398:18)
2017-05-18T12:09:19.858761+00:00 app[web.1]: MongoError: topology was destroyed
2017-05-18T12:09:19.858764+00:00 app[web.1]:     at ReplSet.insert (/app/node_modules/mongodb-core/lib/topologies/replset.js:1025:47)
2017-05-18T12:09:19.858766+00:00 app[web.1]:     at executeCommands (/app/node_modules/mongodb/lib/bulk/ordered.js:455:23)
2017-05-18T12:09:19.858766+00:00 app[web.1]:     at OrderedBulkOperation.execute (/app/node_modules/mongodb/lib/bulk/ordered.js:508:10)
2017-05-18T12:09:19.858767+00:00 app[web.1]:     at bulkWrite (/app/node_modules/mongodb/lib/collection.js:664:8)
2017-05-18T12:09:19.858768+00:00 app[web.1]:     at Collection.insertMany (/app/node_modules/mongodb/lib/collection.js:534:44)
2017-05-18T12:09:19.858769+00:00 app[web.1]:     at /app/index.js:121:41
2017-05-18T12:09:19.858768+00:00 app[web.1]:     at Collection.insert (/app/node_modules/mongodb/lib/collection.js:836:15)
2017-05-18T12:09:19.858770+00:00 app[web.1]:     at handleCallback (/app/node_modules/mongodb/lib/utils.js:120:56)
2017-05-18T12:09:19.858771+00:00 app[web.1]:     at /app/node_modules/mongodb/lib/cursor.js:860:16

Please help me to solve this.



via brain_team

No comments:

Post a Comment