猫鼬承诺错误 [英] Mongoose Promise error

查看:73
本文介绍了猫鼬承诺错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使添加了原始承诺后,保存时仍会抛出此错误.

This is the error which is still being thrown when saving even after adding native promise.

(节点:5604)已弃用警告:猫鼬:mpromise(猫鼬的默认诺言库)已过时,请插入您自己的诺言库: 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);
    });

我同时尝试了蓝鸟和q,仍然没有找到解决办法. 下面是我保存此代码的代码,出现以下弃用警告..

I have tried both bluebird & q, still haven't found a fix for this. Below is the code when I save this, the following deprecation warning shows up..

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);
            });

此错误在新版本的猫鼬4.8.1上发生,但在4.7.6的猫鼬版本上可以正常工作.

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;,我也有同样的警告.

Despite using mongoose.Promise = global.Promise; before mongoose.connect(...), I had the same warning.

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

I discovered, that I initialized mongoose connection in one file:

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.Promise = global.Promise;,因此,警告消失了.

But I imported mongoose in another file too (where mongoose scheme was described), so I added mongoose.Promise = global.Promise; in second file too, as a result of it, the warning disappeared.

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

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

也许你有同样的情况.

这篇关于猫鼬承诺错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆