Azure Cosmos DB:TypeError:无法读取未定义

时间:2017-05-24 03:57:37

标签: node.js mongodb azure azure-cosmosdb

我正在使用带有mongodb后端的Azure Cosmos DB。我一直收到这个错误。

Exception has occurred: Error
TypeError: Cannot read property 'electionId' of undefined
at ReplSetState.update (c:\dev\3dshoppers.backend\node_modules\mongodb-core\lib\topologies\replset_state.js:371:70)
at Server._handleEvent (c:\dev\3dshoppers.backend\node_modules\mongodb-core\lib\topologies\replset.js:421:45)
at Server.g (events.js:291:16)
at emitOne (events.js:96:13)
at Server.emit (events.js:188:7)
at c:\dev\3dshoppers.backend\node_modules\mongodb-core\lib\topologies\server.js:297:14
at c:\dev\3dshoppers.backend\node_modules\mongodb-core\lib\connection\pool.js:457:18
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)

我正在使用mongodb@2.2.19。如果我将npm包更新到最新版本,我会得到不同的错误('MongoError:pool destroyed')。我认为Cosmos DB存在一些问题......

这发生在replset_state.js的这一行。

var currentElectionId = self.set[self.primary.name.toLowerCase()].electionId;

编辑:我能够通过以下两个文件重现该错误。

server.js

// packages
const express = require('express');
const http = require('http');
const MongoClient = require('mongodb').MongoClient;

// express app
const app = express();
// mongodb
const mongoConnectionString = process.env.MONGODB_CONNECTION_STRING;
const mongoOptions =
{
    connectTimeoutMS: 0
}

var mongoDB;
var myCollection;

MongoClient.connect(mongoConnectionString, (err, db) => {

    if (err) throw err;

    mongoDB = db.db('testdb');

    mongoDB.collection('cars', (err, collection) => {            
            if (err) throw err;            
            myCollection = collection;
        });

        http.createServer(app).listen(process.env.PORT || 3000);
});

的package.json

{
    "name": "mongodb-debug",
    "description": "mongodb debugging",
    "version": "0.0.1",
    "private": true,
    "license": "MIT",
    "author": "me",
    "engines": {
        "node": "6.9.3"
    },
    "dependencies": {
        "express": "4.15.2",
        "mongodb": "2.2.19"
    },
    "scripts": {
        "start": "node server.js",
        "test": "mocha"
    }
}

2 个答案:

答案 0 :(得分:4)

如果您没有使用副本集并且只有一个Cosmos数据库实例,则可以通过从连接字符串中删除&replicaSet=globaldb来避免此问题。

类似的问题可以在这里找到:MongoError: Pool Destroyed

答案 1 :(得分:0)

我有完全相同的错误:

Thu May 25 2017 05:17:57 GMT+0000 (Coordinated Universal Time): Application has thrown an uncaught exception and is terminated:
TypeError: Cannot read property 'electionId' of undefined
    at ReplSetState.update (D:\home\site\wwwroot\node_modules\mongoose\node_modules\mongodb-core\lib\topologies\replset_state.js:353:70)
    at Server.<anonymous> (D:\home\site\wwwroot\node_modules\mongoose\node_modules\mongodb-core\lib\topologies\replset.js:393:45)
    at Server.g (events.js:291:16)
    at emitOne (events.js:96:13)
    at Server.emit (events.js:188:7)
    at D:\home\site\wwwroot\node_modules\mongoose\node_modules\mongodb-core\lib\topologies\server.js:280:14
    at D:\home\site\wwwroot\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:455:18
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)
TypeError: Cannot read property 'electionId' of undefined
    at ReplSetState.update (D:\home\site\wwwroot\node_modules\mongoose\node_modules\mongodb-core\lib\topologies\replset_state.js:353:70)
    at Server.<anonymous> (D:\home\site\wwwroot\node_modules\mongoose\node_modules\mongodb-core\lib\topologies\replset.js:393:45)
    at Server.g (events.js:291:16)
    at emitOne (events.js:96:13)
    at Server.emit (events.js:188:7)
    at D:\home\site\wwwroot\node_modules\mongoose\node_modules\mongodb-core\lib\topologies\server.js:280:14
    at D:\home\site\wwwroot\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:455:18
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

也在我的node-js应用程序中使用cosmos-db。 连接工作正常,因为我创建的所有集合

hier是package.json和完整的项目

https://github.com/aumanjoa/chronas-community/blob/master/package.json#L48 https://github.com/aumanjoa/chronas-community/