(node:3341) DeprecationWarning: Mongoose: mpromise

后端 未结 8 1831
忘了有多久
忘了有多久 2020-11-27 11:19

I\'m trying to develop a class on the top of the mongoose with my custom methods, so I extended the mongoose with my own class but when I invoke to create a new car method i

相关标签:
8条回答
  • 2020-11-27 12:06

    Here's what worked for me to clear up the issue, after reading docs: http://mongoosejs.com/docs/promises.html

    The example in the doc is using the bluebird promise library but I chose to go with native ES6 promises.

    In the file where I'm calling mongoose.connect:

    mongoose.Promise = global.Promise;
    mongoose.connect('mongodb://10.7.0.3:27107/data/db');
    

    [EDIT: Thanks to @SylonZero for bringing up a performance flaw in my answer. Since this answer is so greatly viewed, I feel a sense of duty to make this edit and to encourage the use of bluebird instead of native promises. Please read the answer below this one for more educated and experienced details. ]

    0 讨论(0)
  • 2020-11-27 12:06

    did you try this? For Example :

    const mongoose = require('mongoose')
    mongoose.Promise = global.Promise // <--
    const Schema = mongoose.Schema
    const UserSchema = new Schema({
      name: String,
    })
    const User = mongoose.model('user', UserSchema)
    module.exports = User
    

    if you create a model from a mongoose instance who's promise wasn't redefined - every query on this model would throw the warning.

    0 讨论(0)
提交回复
热议问题