development

(node ​​: 3341) DeprecationWarning : 몽구스 : mpromise

big-blog 2020. 9. 10. 08:12
반응형

(node ​​: 3341) DeprecationWarning : 몽구스 : mpromise


내 사용자 지정 메서드를 사용하여 몽구스 위에 클래스를 개발하려고하므로 몽구스를 내 클래스로 확장했지만 새 자동차 메서드를 만들기 위해 호출하면 작동하지만 스트립과 오류가 발생합니다. 내가 뭘 하려는지보세요.

이 경고를 받고 있습니다

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

내가 한 후에

driver.createCar({
      carName: 'jeep',
      availableSeats: 4,
    }, callback);

driver는 Driver 클래스의 인스턴스입니다.

const carSchema = new Schema({
  carName: String,
  availableSeats: Number,
  createdOn: { type: Date, default: Date.now },
});
const driverSchema = new Schema({
 email: String,
 name: String,
 city: String,
 phoneNumber: String,
 cars: [carSchema],
 userId: {
   type: Schema.Types.ObjectId,
   required: true,
 },
createdOn: { type: Date, default: Date.now },
});
const DriverModel = mongoose.model('Driver', driverSchema);

class Driver extends DriverModel {
  getCurrentDate() {
  return moment().format();
}
create(cb) {
  // save driver
  this.createdOn = this.getCurrentDate();
  this.save(cb);
}
remove(cb) {
  super.remove({
  _id: this._id,
 }, cb);
}
createCar(carData, cb) {
  this.cars.push(carData);
  this.save(cb);
}
getCars() {
  return this.cars;
 }
}

내가 뭘 잘못하고 있는지에 대한 생각?


문서를 읽은 후 문제를 해결하는 데 도움이 된 것은 다음과 같습니다. http://mongoosejs.com/docs/promises.html

문서의 예제는 bluebird promise 라이브러리를 사용하고 있지만 네이티브 ES6 promise를 사용하기로 선택했습니다.

내가 부르는 파일에서 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. ]


While the answer above is accurate and works, you have to account for the issue of Performance if you have a real, production Node app.

The solution above will use native ES6 promises - which are 4X slower than bluebird in the benchmarks I've shared below. This could dramatically affect the performance of an API written in Node and using MongoDB.

I recommend using Bluebird:

// Assuming you store the library in a var called mongoose
var mongoose = require('mongoose');

// Just add bluebird to your package.json, and then the following line should work
mongoose.Promise = require('bluebird');

Benchmark Results

Platform: (using latest Node at time of writing)

  • Linux 4.4.0-59-generic x64
  • Node.JS 6.9.4
  • V8 5.1.281.89
  • Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz × 4
  • 16 GB RAM with 500 GB SSD

    | file                                      | time(ms) | memory(MB) |
    |-------------------------------------------|----------|------------|
    | callbacks-baseline.js                     | 114      | 25.09      |
    | callbacks-suguru03-neo-async-waterfall.js | 152      | 32.98      |
    | promises-bluebird-generator.js            | 208      | 29.89      |
    | promises-bluebird.js                      | 223      | 45.47      |
    | promises-cujojs-when.js                   | 320      | 58.11      |
    | promises-then-promise.js                  | 327      | 64.51      |
    | promises-tildeio-rsvp.js                  | 387      | 85.17      |
    | promises-lvivski-davy.js                  | 396      | 81.18      |
    | callbacks-caolan-async-waterfall.js       | 527      | 97.45      |
    | promises-dfilatov-vow.js                  | 593      | 148.30     |
    | promises-calvinmetcalf-lie.js             | 666      | 122.78     |
    | generators-tj-co.js                       | 885      | 121.71     |
    | promises-obvious-kew.js                   | 920      | 216.08     |
    | promises-ecmascript6-native.js            | 931      | 184.90     |
    | promises-medikoo-deferred.js              | 1412     | 158.38     |
    | streamline-generators.js                  | 1695     | 175.84     |
    | observables-Reactive-Extensions-RxJS.js   | 1739     | 218.96     |
    | streamline-callbacks.js                   | 2668     | 248.61     |
    | promises-kriskowal-q.js                   | 9889     | 410.96     |
    | observables-baconjs-bacon.js.js           | 21636    | 799.09     |
    | observables-pozadi-kefir.js               | 51601    | 151.29     |
    | observables-caolan-highland.js            | 134113   | 387.07     |

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.


var mydb;
var uri = 'mongodb://localhost/user1';
var promise = mongooose.connect(uri,{
      useMongoClient: true,
});
promise.openUri(uri,function(errr,db){
if(errr){
        throw errr;
      }else{
        console.log("Connection Successfull");      
        mydb = db;
      }
});

One needs to have connection with the help of promise in the latest version of mongoose [this is the link][1] [1]: http://mongoosejs.com/docs/promises.html


Just add the second parameter as an object to the connect() method.

mongoose.connect('dbUrl', {
  useMongoClient: true
});

See: http://mongoosejs.com/docs/connections.html#use-mongo-client


I think you have your answer but I use global.promise with error handling

// MongoDB connection
mongoose.Promise = global.Promise;

var promise = mongoose.connect('mongodb://localhost:27017/test_db', {
  useMongoClient: true,
});

promise.then(function(db) {
    console.log("Connected to database!!!");
}, function(err){
    console.log("Error in connecting database " + err);
});

Mongoose 4.8.6

If you catch error like this:

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

You need also to set in options which promise library to use for the driver.

mongoose.Promise = global.Promise
mongoose.connect(uri, { useMongoClient: true, options: { promiseLibrary: mongoose.Promise }})

var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
db = mongoose.connect(env.DATABASE_URI, function(){
  //
})

this work for me.

참고URL : https://stackoverflow.com/questions/38138445/node3341-deprecationwarning-mongoose-mpromise

반응형