我有下面的代码,它请求在现在之前返回所有计划的msg,如果有一些,它将用新日期(根据repeat字段增加其值)更新“计划”集合中的每个文档,如果完成此文档更新,它将在上插入一个新文档”msgs“收藏。
问题是我做了一个过于复杂的代码,它在最后插入了重复的msg。我怀疑在更新循环中的“scheduled”文档的异步请求完成之前,函数“insertscheduledmsgs”被再次调用,因此下次调用该函数时,该计划文档仍具有旧日期。
如何简化并解决此问题?
下面您可以看到整个代码:
function insertScheduledMsgs() {
request.get( {
headers: { 'Authorization':'Bearer ' + token },
url: urlApi + "scheduleBeforeOrAfterNow?msgsPosition=before"
}, function( error, response, body ){
if ( !error && response.statusCode == 200 ) {
results = JSON.parse( body );
for ( let res in results ){
let bodyMsg = "",
bodySchedule = "",
log,
scheduleDate = (Number( results[res].date ) + ( Number( results[res].repeat ) * 60 * 1000)),
date1 = new Date(results[res].date),
date2 = new Date();
bodyMsg += 'user=' + results[res].user;
if ( results[res].media ) {
bodyMsg += '&media=' + results[res].media;
}
bodyMsg += '&txt=' + results[res].txt;
bodyMsg += '&time=' + results[res].time;
bodyMsg += '&layout=' + results[res].layout;
bodyMsg += '&boxes=' + results[res].boxes;
bodySchedule += '_id=' + results[res]._id;
bodySchedule += '&date=' + scheduleDate;
if( date1.setSeconds(0,0) <= date2.setSeconds(0,0) ) {
request.put( {
headers: { 'content-type' : 'application/x-www-form-urlencoded', 'Authorization':'Bearer ' + token },
url: urlApi + "schedule",
body: bodySchedule
}, function( errorSchedule, responseSchedule, body ){
logs({
event: "date updated in scheduled msg",
date: new Date().getTime()
}, token);
if( date1.setSeconds(0,0) === date2.setSeconds(0,0) ) {
request.post( {
headers: { 'content-type' : 'application/x-www-form-urlencoded', 'Authorization':'Bearer ' + token },
url: urlApi + "msgs",
body: bodyMsg
}, function( errorMsg, responseMsg, body ){
logs({
event: "scheduled msg sent to monitor / tv",
date: new Date().getTime()
}, token);
if ( (Number(res) + 1) === results.length ) {
insertScheduledMsgs();
}
} );
} else {
if ( (Number(res) + 1) === results.length ) {
insertScheduledMsgs();
}
}
} );
} else {
if ( (Number(res) + 1) === results.length ) {
insertScheduledMsgs();
}
}
};
} else {
insertScheduledMsgs();
}
} );
};