gpt4 book ai didi

node.js - DeprecationWarning : `open()` is deprecated in mongoose >= 4. 11.0,使用 `openUri()`代替,或者设置 `useMongoClient`

转载 作者:太空宇宙 更新时间:2023-11-03 23:21:39 26 4
gpt4 key购买 nike

我在 Node.js 项目中使用 MongoDB,当我在项目中运行 node app.js 时,出现如下问题:

DeprecationWarning: open() is deprecated in mongoose >= 4.11.0,use openUri() instead, or set the useMongoClient option if using connect() or createConnection()

我的核心代码:

var mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection

db.on('error', console.error.bind(console, 'connect error:'));
db.once('open', function() {
console.log('connect success');
})

最佳答案

来自http://mongoosejs.com/docs/connections.html :

const options = {
useMongoClient: true,
autoIndex: false, // Don't build indexes
reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10, // Maintain up to 10 socket connections
// If not connected, return errors immediately rather than waiting for reconnect
bufferMaxEntries: 0
};
mongoose.connect(uri, options);

连接mongodb时,应添加{useMongoClient:true}:

mongoose.connect('mongodb://localhost/test',{useMongoClient:true})

关于node.js - DeprecationWarning : `open()` is deprecated in mongoose >= 4. 11.0,使用 `openUri()`代替,或者设置 `useMongoClient`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48981109/

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