简体   繁体   English

nodejs永远运行

[英]nodejs keeps running forever

I'm writing a script in nodejs that will collect some transactions in the mongodb, post it to a API and the it should be done (and go back to cli). 我正在用nodejs编写脚本,该脚本将在mongodb中收集一些事务,将其发布到API并应完成(然后返回cli)。 I have something like this: 我有这样的事情:

var request = require('request');
var mongoose = require('mongoose');
var Transaction = require('./models/transaction');
var send_it = function (transaction) {
    request(URL, {token: transaction.token}, function (err, response,     body) {
        console.info('Processed');
    });
};
var process_transactions = function (err, transactions) {
    for (var i = 0; i < transactions.length; i++) {
        send_it(transactions[i]);
    }
};

mongoose.connect('mongodb://localhost/forsocios');
Transaction.find({captured: false}, process_transactions);

The problem is: this script keeps running forever even when there is no more transactions to post. 问题是:即使没有更多事务要发布,此脚本也会永远运行。 Why is that? 这是为什么? And how can I make it die (process.exit(0)) when there is no more transactions to process? 当没有更多事务需要处理时,如何使它死掉(process.exit(0))?

最好的主意是学习诺言和异步/等待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM