Aud*_*rez 87 mongoose mongodb node.js
我正在尝试使用我的自定义方法在猫鼬的顶部开发一个类,所以我用我自己的类扩展了猫鼬但是当我调用创建一个新的汽车方法时它可以工作但它的条带和错误,在这里我让你看看我想做什么.
我收到了这个警告
(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
Run Code Online (Sandbox Code Playgroud)
在我之后
driver.createCar({
carName: 'jeep',
availableSeats: 4,
}, callback);
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
关于我做错什么的任何想法?
Hun*_*ter 237
在阅读完文档之后,以下是解决问题的方法:http: //mongoosejs.com/docs/promises.html
文档中的示例是使用bluebird promise库,但我选择使用本机ES6承诺.
在我正在调用的文件中mongoose.connect:
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://10.7.0.3:27107/data/db');
Run Code Online (Sandbox Code Playgroud)
[编辑:感谢@SylonZero在我的回答中提出了一个性能缺陷.由于这个答案被大大地看待,我觉得有责任进行编辑并鼓励使用bluebird而不是本机的承诺.请阅读下面的答案,了解更多有经验和有经验的细节.]
Syl*_*ero 70
虽然上面的答案是准确且有效的,但如果您有一个真实的生产节点应用程序,则必须考虑性能问题.
上面的解决方案将使用本机ES6承诺 - 在我下面分享的基准测试中,它比蓝鸟慢4倍.这可能会极大地影响在Node中编写并使用MongoDB的API的性能.
我建议使用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');
Run Code Online (Sandbox Code Playgroud)
基准测试结果
平台:(在撰写本文时使用最新的节点)
| 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 |
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54238 次 |
| 最近记录: |