本文介绍了为什么生成实例池的MongoDB节点驱动程序会销毁错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行以下代码时,我收到错误消息" MongoError:服务器实例池已损坏".知道为什么或如何解决这个问题吗?

var csv = require('./importer.js');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var url = 'mongodb://.....';


MongoClient.connect(url, function(err, db) {

    assert.equal(null, err);
    console.log("Connected correctly to server.");

    csv.foreach('data/airports.csv', function(airport){
        db.collection('airports').insertOne(airport, function(err, result) {
            if(err) {
                console.log(err)
            } else {
                console.log("Inserted: " + airport.ident);
            }
        });
    });

    db.close();
});
解决方案

csv.foreachinsertOne调用(大概)都是异步的,因此您要在插入完成之前调用db.close().

您需要提出一种等待调用db.close()的方法,直到调用了所有插入的回调.如何执行此操作取决于您的csv模块如何工作,但是使用异步模块之类的方法可以帮助进行异步流控制. /p>

When I run the following code I am getting the error message 'MongoError: server instance pool was destroyed'. Any idea why or how to fix this?

var csv = require('./importer.js');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var url = 'mongodb://.....';


MongoClient.connect(url, function(err, db) {

    assert.equal(null, err);
    console.log("Connected correctly to server.");

    csv.foreach('data/airports.csv', function(airport){
        db.collection('airports').insertOne(airport, function(err, result) {
            if(err) {
                console.log(err)
            } else {
                console.log("Inserted: " + airport.ident);
            }
        });
    });

    db.close();
});
解决方案

csv.foreach and the insertOne calls are (presumably) both async, so you're calling db.close() before your inserts have completed.

You need to come up with a way of waiting to call db.close() until all your inserts' callbacks have been called. How to do that depends on how your csv module works, but using something like the async module can help with the async flow control.

这篇关于为什么生成实例池的MongoDB节点驱动程序会销毁错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 19:28