【发布时间】:2017-04-27 19:49:13
【问题描述】:
鉴于这种情况:
-UserA 发布了一个Campaign,上传了给定数量的照片。
-其他用户可以上传照片
-UserA可以接受上传的照片
-照片的所有者为每张接受的照片获得 1 个硬币
-对于每张接受的照片,活动中可能的照片数量都会减少。
我解决了这样的“接受照片”功能(伪代码):
accept: function (req, res, next) {
var photos = req.param('photos'); // [1, 2, 5]
var campaign_id = req.param('campaign_id'); //1
Photo.update(photos, {status:'accepted'}).exec(function afterwards(err, updatedPhotos)
{
//get the users Ids from the updatedPhotos
// also know how many photos of each user were accepted
User.update(users){
//increase the amount of coins of this user
}
Campaign.update(amountOfAcceptedPhotos){
//decrease the amount of selectable photos.
}
})
}
首先一切都很顺利,直到我注意到当您使用相同的 photoID 快速发送相同的请求时,用户将获得同一张照片的多个硬币!我怎样才能防止这种情况?
如果您想查看确切的功能,这是整个代码的链接: https://jsfiddle.net/zsmuoh0x/
【问题讨论】:
标签: javascript node.js mongodb transactions sails.js