var job = new cronJob('* * * * * *', function () {
//find and send message using cron job
Quicksms.findOne({
attributes: ['id', 'phoneno', 'sender', 'message', 'isSent'],
raw: true
}).then(data => {
console.log('data:', data);
// var filteredData = datas.filter(data => data.isSent === 0);
// console.log('filteredData:', filteredData);
// for (var i = 0; i < data.length; i++) {
// if (data.isSent === 0) {
var uri = '/api/user=test&password=test&sender=' +
data.sender + '&SMSText=' + data.message + '&GSM=' + data.phoneno;
var sms = encodeURI(uri);
var options = {
host: 'test.com',
port: 80,
path: sms,
method: 'GET'
};
http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
}).end();
//update into 1 true:
Quicksms.update({
isSent: 'true'
}, {
where: {
id: data.id
}
})
// }
// }
//store in log
QuicksmsLog.create({
phoneno: data.phoneno,
sender: data.sender,
message: data.message,
isSent: data.isSent
}).then(function (data) {
if (data) {
console.log("successfully stored in log");
} else {
console.log("failed");
}
})
//destroy message after sending
Quicksms.destroy({
where: {
isSent: 1
}
}).then(delData => {
console.log('cronJob deleted successfully.', delData);
});
});
}, function () {
console.log('DelCron Job finished.');
}, true, 'Asia/Calcutta');
以上代码作为异步订单工作 异步订单:
1.Quicksms.findone
2.Quicksms.删除
3.Quicksms.Update
4.Quicksmslogs.Create
这个作为异步工作,我想按顺序工作!
如何以同步方式做到这一点?帮助我
异步任务有什么用?在nodejs中,我是新手!帮助我。
最佳答案
您可以通过使用异步等待操作来做到这一点。尝试运行以下代码。
var job = new cronJob('* * * * * *', async function () {
//find and send message using cron job
await Quicksms.findOne({
attributes: ['id', 'phoneno', 'sender', 'message', 'isSent'],
raw: true
}).then(data => {
console.log('data:', data);
// var filteredData = datas.filter(data => data.isSent === 0);
// console.log('filteredData:', filteredData);
// for (var i = 0; i < data.length; i++) {
// if (data.isSent === 0) {
var uri = '/api/user=test&password=test&sender=' +
data.sender + '&SMSText=' + data.message + '&GSM=' + data.phoneno;
var sms = encodeURI(uri);
var options = {
host: 'test.com',
port: 80,
path: sms,
method: 'GET'
};
http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
}).end();
//update into 1 true:
await Quicksms.update({
isSent: 'true'
}, {
where: {
id: data.id
}
})
// }
// }
//store in log
await QuicksmsLog.create({
phoneno: data.phoneno,
sender: data.sender,
message: data.message,
isSent: data.isSent
}).then(function (data) {
if (data) {
console.log("successfully stored in log");
} else {
console.log("failed");
}
})
//destroy message after sending
await Quicksms.destroy({
where: {
isSent: 1
}
}).then(delData => {
console.log('cronJob deleted successfully.', delData);
});
});
}, function () {
console.log('DelCron Job finished.');
}, true, 'Asia/Calcutta');
关于node.js - 我想在nodejs中按顺序同步执行下面的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47524342/