Я хочу, чтобы пользователи моего приложения увеличивали value
только один раз. Следующий код не работает, потому что update
в мангусте асинхронен. Моя проверка if(user.valueIncremented)
не подходит для следующих последовательных запросов к этому api, потому что valueIncremented
не обновляется в этой точке, но будет находиться в следующем цикле событий. Итак, что мне делать, добрые господа?Атомное обновление в Mongoose
var Account = new Schema({
\t _id: Schema.Types.ObjectId,
\t value: {type: Number, default: 0},
\t valueIncremented: {type: Boolean, default: false}
});
router.post('/incrementValue', function(req, res, next) {
\t var user = req.user;
\t if(user.valueIncremented) {
\t \t return next();
\t }
\t else {
\t \t incrementValue();
\t }
\t function incrementValue(){
\t \t var condition = {
\t \t \t _id: user._id;
\t \t }
\t \t var update = {
\t \t \t $inc: {
\t \t \t \t value: 1
\t \t \t },
\t \t \t $set: {
\t \t \t \t valueIncremented: true
\t \t \t }
\t \t }
\t \t Account.update(condition, update).exec();
\t }
}) \t
Где вы настройки req.user.valueIncremented? И как это точно проваливается? –