(节点:3341)弃用警告:猫鼬:承诺

新手上路,请多包涵

我正在尝试使用我的自定义方法在 mongoose 上开发一个类,所以我用我自己的类扩展了 mongoose,但是当我调用创建一个新的 car 方法时它可以工作,但是它的条带和错误,在这里我让你看看我想要做什么。

我收到此警告

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

关于我做错了什么有什么想法吗?

原文由 Audel O. Gutierrez 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 608
2 个回答

在阅读文档后,这对我来说是解决问题的方法: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');

[编辑:感谢@SylonZero 在我的回答中提出了性能缺陷。由于这个答案被广泛关注,我感到有责任进行此编辑并鼓励使用 bluebird 而不是原生承诺。请阅读下面的答案以获取更多受过教育和经验丰富的详细信息。 ]

原文由 Hunter Lester 发布,翻译遵循 CC BY-SA 3.0 许可协议

虽然上面的答案是准确且有效的,但如果您有一个真正的生产节点应用程序,则必须考虑 性能 问题。

上面的解决方案将使用原生 ES6 promises——在我下面分享的基准测试中,它比 bluebird 慢 4 倍。这可能会极大地影响用 Node 编写并使用 MongoDB 的 API 的性能。

我推荐使用蓝鸟:

 // 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');

基准测试结果

平台:(在撰写本文时使用最新的 Node)

  • Linux 4.4.0-59-通用 x64
  • 节点.JS 6.9.4
  • V8 5.1.281.89
  • Intel® Core™ i7-6500U CPU @ 2.50GHz × 4
  • 16 GB 内存和 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     |

原文由 SylonZero 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题