node.js - 我收到重复的结果,因为 nodejs 在发送 200 响应之前就将其判断为发送失败
问题描述
当应用参与奖励事件时,奖励服务器将结果发送到应用服务器。如果没有 200 响应,奖励公司服务器会在一定时间内重复发送结果。
如果应用服务器没有响应 200,奖励服务器会重复发送结果。
所有获得的积分都会被记录下来。检查是否有带有事件ID的点记录,如果没有点记录,记录点并以200响应。问题是即使在第一个200响应之前,就判断为传输失败,结果再次发送,因此具有相同事件 ID 的多个点正在累积。
我该怎么办?有没有办法在应用服务器上处理这个问题?
db.collection('pointHistory').doc(uid).collection('pointHistory').add(newPointHistory).then(ref => {
db.collection('users').doc(uid).update({point: addTotalPoint});
db.collection('offerwallHistory').doc(uid).collection('offerwallHistory').add(offerwallHistory_data);
db.collection('adminPointHistory').add(adminPointHistory_data);
setDailyPointStatus(reward, 12);
setOtherPersonalPointStatus(reward, uid, "dailyPersonalPointStatus");
setOtherPersonalPointStatus(reward, uid, "weeklyPersonalPointStatus");
setOtherPersonalPointStatus(reward, uid, "monthlyPersonalPointStatus");
return res.send(event_id + ":OK").status(200);
}).catch(err => {
console.log(err);
return null;
});
return res.send(event_id + ":OK").status(200);
解决方案
是的,你做错了。您使用的 promise(then/catch) 不会等待所有过程完成。您应该尝试了解更多有关 async / await 的信息,以使您的代码更清晰易读,并且您可以像刚才一样使用它,但没有返回 return res.send(event_id + ":OK").status(200); 在末尾。例如:
try {
await db.collection('pointHistory').doc(uid).collection('pointHistory').add(newPointHistory)
await db.collection('users').doc(uid).update({point: addTotalPoint});
db.collection('offerwallHistory').doc(uid).collection('offerwallHistory').add(offerwallHistory_data);
db.collection('adminPointHistory').add(adminPointHistory_data);
setDailyPointStatus(reward, 12);
setOtherPersonalPointStatus(reward, uid, "dailyPersonalPointStatus");
setOtherPersonalPointStatus(reward, uid, "weeklyPersonalPointStatus");
setOtherPersonalPointStatus(reward, uid, "monthlyPersonalPointStatus");
return res.send(event_id + ":OK").status(200);
} catch(error) {
return null;
}
您需要像这样包装函数:
async function myFunc() {}
推荐阅读
- kubernetes - AZDATA BDC CREATE 卡住了。控制容器挂起。NFS PVC 上的调度错误
- robotframework - Robot Framework 是否会自动导入您的机器人测试所在的当前目录?
- react-native - 如何从另一个屏幕组件打开抽屉,以在内容组件的道具中使用另一个屏幕做出本机反应
- android - 使用 LifecycleObserver 的生命周期感知组件如何感知屏幕方向的变化
- python - 索引中带有 nan 的 Pandas 数据框
- github - 将文件从一个存储库复制到另一个存储库的 github 操作
- javascript - 在 react-chartjs-2 折线图上添加条形图(垂直线)
- firebase - “如果 request.auth.uid != null”作为 Firestore 中的安全规则有多公开?
- php - WordPress 联系表 7 不适用于 Xampp 中的 PHP 7.2
- javascript - 从 javascript 中的扁平化 JSON 创建嵌套 json