Mongo's Cursor.nextObject Sometimes Erroneously Returns Null?

后端 未结 1 1854
孤城傲影
孤城傲影 2021-01-21 21:33

I\'m combining async.queue and Cursor.nextObject to iterate over a cursor and do some asynchronous work against the returned documents.

There\'

相关标签:
1条回答
  • 2021-01-21 22:22

    I'm still not sure what's causing the pause, but it seems that is the culprit.

    During the pause, Cursor.nextObject is getting called several times before the first returns. Some of these calls are returning null. The solution is to make sure Cursor.nextObject is never called concurrently.

    if (this.cursor && !this.cursor_exec && this.length() < this.concurrency) {
        this.cursor_exec = true;
        this.cursor.nextObject(function(err, item) {
            console.log(this.name + ': ' + (item ? item._id : null) + ' ' + (err ? err : null));
            this.cursor_exec = false;
            if (item) {
                this.push(item);
            } else {
                delete this.cursor;
            }
        }.bind(this));
    }
    
    0 讨论(0)
提交回复
热议问题