gpt4 book ai didi

node.js - Mongoose 没有方法 `connect`

转载 作者:可可西里 更新时间:2023-11-01 09:56:29 24 4
gpt4 key购买 nike

我使用 npm 在 node.js 上安装了 mongoose。当我尝试连接此代码时:

mongoose = require('mongoose/').Mongoose
@db = mongoose.connect("mongodb://localhost/test")

我收到这个错误:

node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Object function Mongoose() {
this.connections = [];
this.plugins = [];
this.models = {};
this.modelSchemas = {};
this.options = {};
this.createConnection(); // default connection
} has no method 'connect'
at new ChatService (/home/lolcoder/workspace/sampleApp/src/services/dbService.js:12:26)
at Object.<anonymous> (/home/lolcoder/workspace/sampleApp/src/app.js:40:17)
at Object.<anonymous> (/home/lolcoder/workspace/sampleApp/src/app.js:44:4)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Array.0 (module.js:479:10)
at EventEmitter._tickCallback (node.js:192:40)

当在终端中执行 mongo 时,我得到:

MongoDB shell version: 2.0.2
connecting to: test

谁知道为什么mongoose对象中没有connect()方法?

最佳答案

代替

mongoose = require('mongoose/').Mongoose;

使用

mongoose = require('mongoose/');

关于node.js - Mongoose 没有方法 `connect`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9386101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com