简体   繁体   中英

How to store both the JSON object to mongodb, if two device hit the api at same time in nodejs

In My project I have two hardware device. Each hitting my API with the JSON object every two minutes. If it hit different time, my controller stores both the JSON object mongodb.

BUT if both the device hit the API at same time, it stores 1st object to database for second one it is giving error.

If two device hit "pushSensorData" at same time that time am getting mongo error.

 pushSensorData: function(req, res) { receievedMessage = req.query; console.log('received message ', receievedMessage); MongoClient.connect(mongoUrl, function(err, db) { db.collection('sensor').find({ "sMacId": receievedMessage.MAC }).toArray(function(error, response) { ievedMessage.timestamp = new Date(receievedMessage.timestamp); receievedMessage["sensorMacId"] = receievedMessage.MAC.toString(); if (receievedMessage.sensorMacId != null) { receievedMessage["occupancy"] = parseInt(receievedMessage.OC); receievedMessage["ambientLight"] = parseInt(receievedMessage.ALS); receievedMessage["power"] = parseInt(receievedMessage.POW); receievedMessage["timestamp"] = receievedMessage.timestamp; if (isNaN(receievedMessage.occupancy)) receievedMessage["occupancy"] = 0; if (isNaN(receievedMessage.ambientLight)) receievedMessage["ambientLight"] = 0; if (isNaN(receievedMessage.power)) receievedMessage["power"] = 0; delete receievedMessage.MAC; delete receievedMessage.OC; delete receievedMessage.ALS; delete receievedMessage.POW; insertDocument(db, function() { db.close(); }); } else { db.close(); } }) }); var insertDocument = function(db, callback) { db.collection('sensordata').insertMany([receievedMessage], function(err, response) { console.log("Inserted", response); }); }; }, }; 

this is the error am getting TypeError: Cannot read property 'toString' of undefined 0|app | at /home/sp_mean/smartpower/api/controllers/SensorMessageController.js:63:71 0|app | at handleCallback (/home/sp_mean/smartpower/node_modules/mongodb/lib/utils.js:95:56) 0|app | at /home/sp_mean/smartpower/node_modules/mongodb/lib/cursor.js:852:16 0|app | at handleCallback (/home/sp_mean/smartpower/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:171:5) 0|app | at setCursorNotified (/home/sp_mean/smartpower/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:516:3) 0|app | at /home/sp_mean/smartpower/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:591:16 0|app | at queryCallback (/home/sp_mean/smartpower/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:232:18) 0|app | at /home/sp_mean/smartpower/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:455:18 0|app | at _combinedTickCallback (internal/process/next_tick.js:67:7) 0|app | at process._tickDomainCallback (internal/process/next_tick.js:122:9) PM2 | App [app] with id [0] and pid [38409], exited with code [1] via signal [SIGINT] PM2 | Starting execution sequence in -fork mode- for app name:app id:0

if we look closely in your code, we can see that you're closing the database connection after the operation using db.close(); So, that throws an error in the second request execution, either you omit to close the database (it isn't a big deal actually), the process existing will handle this for you either way or use sessions to separate requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM