node.js - 我想在nodejs中按顺序同步执行下面的代码

标签 node.js

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/

相关文章:

node.js - 负载测试 Bot 构建器 bot

javascript - 在此模块外的 Node.js 模块中模拟构造函数(或其他函数)

node.js - 如何在路径中创建文件夹?

node.js - Firebase Functions npm install 总是卡住

node.js - socket.io 广播太多数据包

mysql - 一对多sequelizejs ORM未能得到结果

javascript - 向给定 channel 中的所有用户发送数据 - Nodejs (Socket.io)

templates - 从现有的 JS Node 上下文中编译 Jade

javascript - 我想用 JavaScript 将对象数组转换为对象

node.js - npm-cache 目录下,_cacache 以外的文件夹是做什么用的?