简体   繁体   中英

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). 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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