简体   繁体   English

如果两个设备同时在Node.js中命中api,如何将两个JSON对象都存储到mongodb

[英]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. 每隔两分钟就会用JSON对象访问我的API。 If it hit different time, my controller stores both the JSON object mongodb. 如果到达其他时间,我的控制器将同时存储两个JSON对象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. 但是,如果两个设备同时按下API,则它将第一个对象存储到数据库中,而第二个对象则给出了错误。

If two device hit "pushSensorData" at same time that time am getting mongo error. 如果两个设备同时按下“ pushSensorData”,则会出现mongo错误。

 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 | 这是获取TypeError的错误:无法读取未定义0 | app的属性'toString'| at /home/sp_mean/smartpower/api/controllers/SensorMessageController.js:63:71 0|app | 在/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 | 在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 | 在/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 | 在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 | 在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 | 在/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 | 在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 | 在/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 | 在_combinedTickCallback(内部/进程/next_tick.js:67:7)处0 | app | at process._tickDomainCallback (internal/process/next_tick.js:122:9) PM2 | 在process._tickDomainCallback(内部/进程/next_tick.js:122:9)中PM2 | App [app] with id [0] and pid [38409], exited with code [1] via signal [SIGINT] PM2 | ID [0]和pid [38409]的应用[app]通过信号[SIGINT] PM2 |代码1退出。 Starting execution sequence in -fork mode- for app name:app id:0 以-fork模式-为应用程序名称开始执行序列:应用程序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(); 如果我们仔细查看您的代码,可以看到您在使用db.close();操作之后正在关闭数据库连接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. 因此,这会在第二次请求执行中引发错误,或者您忽略了关闭数据库(实际上这没什么大不了的),现有的进程将为您处理此问题,或者使用会话来分离请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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