Javascript 如何在mongoose.connect()中传递选项
我正在使用猫鼬,但我需要传递到选项:Javascript 如何在mongoose.connect()中传递选项,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我正在使用猫鼬,但我需要传递到选项: {useNewUrlParser:true} {useUnifiedTopology:true} 但当我通过这两个测试时,它会给出一个错误 DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true
DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser,
pass option { useNewUrlParser: true } to MongoClient.connect.
(node:11212) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
C:\Users\aA\Desktop\database pro\frussproject\node\u modules\mongoose\lib\connection.js:585
抛出新的MongooseError('mongoose.connect()'的第三个参数或'+^Error[MongooseError]:mongoose.connect()'或'mongoose.createConnection()'的第三个参数必须是函数,获得“object”
在mongoose.connect的NativeConnection.Connection.openUri(C:\Users\aA\Desktop\database pro\果树项目\node\u modules\mongoose\lib\error\MongooseError.js:10:11)的新的MongooseError(C:\Users\aA\Desktop\database pro\果树项目\node\u modules\mongoose\lib\Connection.js:585:11)(C:\Users\aA\Desktop\database pro\fruits project\node\u modules\mongoose\lib\index.js:333:15)
在Object.(C:\Users\aA\Desktop\database pro\FruitsProject\app.js:5:10)在Module.Module.编译(internal/modules/cjs/loader.js:1158:30)在Object.Module.扩展..js(internal/modules/cjs/loader.js:1178:10)在Module.load(internal/modules/cjs/cjs/loader.js:1002:32)
在Function.Module.加载(internal/modules/cjs/loader.js:901:14)在Function.executeUserEntryPoint[作为runMain](internal/modules/run_main.js:74:12)
在internal/main/run_main_module.js:18:47{message:`mongoose.connect()`或`mongoose.createConnection()`的第三个参数必须是函数,获得“object”,名称:`MongooseError'}
您需要在.connect()
构造函数中添加这些选项
像这样:
mongoose.connect(“mongodb://localhost:27017/", {
useNewUrlParser:true,
useUnifiedTopology:正确
});
传递选项的方法多种多样。connect
mongoose.set('useNewUrlParser', false);
mongoose.set('useUnifiedTopology', false);
您可以执行上述操作,也可以使用:
C:\Users\aA\Desktop\database pro\FruitsProject\node_modules\mongoose\lib\connection.js:585
throw new MongooseError('3rd parameter to `mongoose.connect()` or ' + ^Error [MongooseError]: 3rd parameter to `mongoose.connect()` or `mongoose.createConnection()` must be a function, got "object"
at new MongooseError (C:\Users\aA\Desktop\database pro\FruitsProject\node_modules\mongoose\lib\error\mongooseError.js:10:11) at NativeConnection.Connection.openUri (C:\Users\aA\Desktop\database pro\FruitsProject\node_modules\mongoose\lib\connection.js:585:11) at Mongoose.connect (C:\Users\aA\Desktop\database pro\FruitsProject\node_modules\mongoose\lib\index.js:333:15)
at Object.<anonymous> (C:\Users\aA\Desktop\database pro\FruitsProject\app.js:5:10) 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 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 { message: '3rd parameter to `mongoose.connect()` or `mongoose.createConnection()` must be a function, got "object"', name: 'MongooseError'}
在连接字符串之后,不带选项
另外,在所有操作完成后,不要忘记通过mongoose.disconnect()
const options = {
useNewUrlParser: true,
useUnifiedTopology: true
};
mongoose.connect("mongodb://localhost:27017/fruitsDB", options);