gpt4 book ai didi

node.js - Mongoose promise 错误

转载 作者:IT老高 更新时间:2023-10-28 12:29:30 26 4
gpt4 key购买 nike

这是添加原生promise后保存时仍然抛出的错误。

(node:5604) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html

mongoose.Promise = global.Promise;
mongoose.connect('mongodb://127.0.0.1/optimusCP')
.then(function () {
console.log('Connected to MONGOD !!');
}).catch(function (err) {
console.log('Failed to establish connection with MONGOD !!');
console.log(err.message);
});

我已尝试过 bluebird 和 q,但仍未找到解决此问题的方法。以下是我保存时的代码,出现以下弃用警告..

var user = new User();
user.email = req.body.email;
user.password = hash;
user.save()
.then(function (user) {
console.log(user);
})
.catch(function (err) {
console.log(err);
});

This error is happening on the new version of mongoose which is 4.8.1, but working fine on 4.7.6 mongoose version.

最佳答案

尽管在 mongoose.connect(...) 之前使用了 mongoose.Promise = global.Promise;,但我收到了同样的警告。

我发现,我在一个文件中初始化了 mongoose 连接:

import mongoose from 'mongoose';

...

// Connect to MongoDB
mongoose.Promise = global.Promise;
mongoose.connect(mongoUri, mongoOptions);
mongoose.connection.on('error', (err) => {
console.error(`MongoDB connection error: ${err}`);
process.exit(1);
});

但我也在另一个文件中导入了 mongoose(其中描述了 mongoose 方案),所以我也在第二个文件中添加了 mongoose.Promise = global.Promise;,如结果,警告消失了。

import mongoose, { Schema } from 'mongoose';
mongoose.Promise = global.Promise;

const UserSchema = new Schema({ ... });

可能你也有同样的情况。

关于node.js - Mongoose promise 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41936968/

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