对不起标题。我真的不知道如何在这里描述我的问题。我的模型中有一个download_count属性。每次从 Android 应用程序成功完成下载时,它都会GET是download_count并用增加的数字来回火PUT请求。
所以我的问题是,设备 A 很有可能得到,并且在设备 A 发回增加的数字之前,设备 B 获得相同的值。
我怎样才能防止这种情况?
为什么要使用 GET 请求来获取 download_count?您不需要这样做来更新 NodeJS 后端中的下载计数。相反,为什么不在下载完成后触发单个 POST 请求,并处理路由中模型中计数的更新?因此,您不必担心任何延迟问题。
这是我的想法,假设您正在使用 MEAN Stack 和 MongooseJS: -
router.post('/update', (req, res)=>{
user.findById(id, function (err, data) { //Send some parameter in the POST request so as to uniquely identify whichever entry you wish to update the download_count of
if (err) console.log(err);
var new_download_count = data.download_count + 1; //Updating count
data.set({ download_count: new_download_count }); //Saving new count
data.save(function (err, updatedData) {
if (err) console.log(err);
res.send(updatedTank); //Indicate new object
});
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句