Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongoose.connection.on(';error';,error=>;)TypeError:无法读取未定义的_Node.js_Mongodb_Mongoose_Dbconnection - Fatal编程技术网

Node.js Mongoose.connection.on(';error';,error=>;)TypeError:无法读取未定义的

Node.js Mongoose.connection.on(';error';,error=>;)TypeError:无法读取未定义的,node.js,mongodb,mongoose,dbconnection,Node.js,Mongodb,Mongoose,Dbconnection,在安装了所有需要的NPM包之后,我得到了这种类型的查询 这是我在db文件夹中的索引文件代码 ../db/index.js 'use strict'; const config = require('../config'); const Mongoose = require('mongoose').connect(config.dbURI); Mongoose.connection.on('error', error => { logger.log('error', 'Mong

在安装了所有需要的NPM包之后,我得到了这种类型的查询

这是我在db文件夹中的索引文件代码 ../db/index.js

'use strict';

const config = require('../config');

const Mongoose = require('mongoose').connect(config.dbURI);

Mongoose.connection.on('error', error => {
    logger.log('error', 'Mongoose connection error: ' + error);
});

module.exports = {
    Mongoose
}
我已经安装了所有需要的软件包。 &错误是

D:\Node\ChatCAT\ChatCAT_Begin>node server
D:\Node\ChatCAT\ChatCAT_Begin\app\db\index.js:15
Mongoose.connection.on('error', error => {
                    ^

TypeError: Cannot read property 'on' of undefined
    at Object.<anonymous> (D:\Node\ChatCAT\ChatCAT_Begin\app\db\index.js:15:21)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (D:\Node\ChatCGV9Jm2u7rmsCe65wKzPTw5jtS38n2tVEGi.js:5:12)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (D:\Node\ChatCAT\ChatCAT_Begin\app\index.js:11:11)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
D:\Node\ChatCAT\ChatCAT\u Begin>节点服务器
D:\Node\ChatCAT\ChatCAT\u Begin\app\db\index.js:15
Mongoose.connection.on('error',error=>{
^
TypeError:无法读取未定义的属性“on”
在对象上。(D:\Node\ChatCAT\ChatCAT\u Begin\app\db\index.js:15:21)
at模块编译(内部/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js(internal/modules/cjs/loader.js:1178:10)
at Module.load(内部/modules/cjs/loader.js:1002:32)
at Function.Module._load(内部/modules/cjs/loader.js:901:14)
at Module.require(内部/modules/cjs/loader.js:1044:19)
根据需要(内部/modules/cjs/helpers.js:77:18)
对象处。(D:\Node\ChatCGV9Jm2u7rmsCe65wKzPTw5jtS38n2tVEGi.js:5:12)
at模块编译(内部/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js(internal/modules/cjs/loader.js:1178:10)
at Module.load(内部/modules/cjs/loader.js:1002:32)
at Function.Module._load(内部/modules/cjs/loader.js:901:14)
at Module.require(内部/modules/cjs/loader.js:1044:19)
根据需要(内部/modules/cjs/helpers.js:77:18)
在对象上。(D:\Node\ChatCAT\ChatCAT\u Begin\app\index.js:11:11)
at模块编译(内部/modules/cjs/loader.js:1158:30)

这是连接MongoDB的正确方法

const mongoose = require('mongoose');

var options = {
    promiseLibrary: require('bluebird'), useNewUrlParser: true, useUnifiedTopology: true
};

var mongodbUri = 'mongodb://127.0.0.1:27017/mydb'
mongoose.connect(mongodbUri, options);
var conn = mongoose.connection;

conn.on('error', console.error.bind(console, 'connection error:'));

检查下面的答案不要粘贴错误的图像,请将其粘贴为纯文本。