// 路由
router.get("/winner", async function (req, res, next) {
let id = Number(req.query.id);
let fileUrlId = req.query.fileUrlId;
if (!id && !fileUrlId) {
return res.send(failModel(2004, false, null));
}
const resGetWC = await getCurrentFileUrl(id);
if (resGetWC.length === 1) {
const tempFileUrl = JSON.parse(resGetWC[0].fileUrl).reduce(
(result, item) => {
if (item.id === fileUrlId) {
item.winnerCount
? (item.winnerCount = item.winnerCount + 1)
: (item.winnerCount = 1);
}
result.push(item);
return result;
},
[]
);
const resultUpdate = await updateWinner(
id,
JSON.stringify(tempFileUrl)
);
if (resultUpdate?.affectedRows === 1) {
return res.send(successModel());
}
return res.send(failModel(-1, false, null));
}
return res.send(failModel(-1, false, null));
});
// updateWinner
const updateWinner = async (id, fileUrl) => {
const resultGet = await getCurrentWinnerCount(id);
if (resultGet.length === 1) {
const sql = `
UPDATE vs_make_lists
SET fileUrl = ${escapeSqlString(fileUrl)}, winnerCount ='${
resultGet[0].winnerCount ? resultGet[0].winnerCount + 1 : 1
}'
WHERE list_id = ${id}
`;
return execSQL(sql);
}
return;
};
const escapeSqlString = str => {
return mysql.escape(str);
};
问题:
- 解析JSON JSON.parse(resGetWC[0].fileUrl)
- 使用reduce 添加winnerCount 字段
- 调用updateWinner 传JSON.stringify(tempFileUrl)
- 插入数据库的时候该 fileUrl JSON字段并没有包含 winnerCunt字段
- 再次调用的时候 fileUrl JSON字段中包含 winnerCunt字段
本地数据库是没有复现出来的,线上的时候会出现此问题