我的代码如下所示:
let Order = require('./model/order.js');
const updateOrderStatus = (orderId, bookingId, orderStatus) => {
return new Promise((resolve, reject) => {
let session;
Order.startSession((_session)=>{
session = _session;
session.startTransaction();
Order.findOne({order_generated_id: orderId},{session:session}).populate('rx1').exec((err, info) => {
if (!err) {
info.order_ref_id = bookingId;
info.order_status = orderStatus;
info.created_at_local_time = moment.utc(new Date(), "YYYY-MM-DD HH").utcOffset(330).format('YYYY-MM-DD HH:mm:ss');
info.updated_at_local_time = moment.utc(new Date(), "YYYY-MM-DD HH").utcOffset(330).format('YYYY-MM-DD HH:mm:ss');
info.save().then((orderInfo) => {
session.commitTransaction();
resolve(orderInfo);
})
}
else {
reject(err);
}
})
})
});
};
Order.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const orders = new Schema({
order_ref_id: {type: String, require: true},
order_generated_id: {type: String, require: false, sparse: true, unique: true},
created_by: {type: String, require: true},
modified_by: {type: String, require: true},
created_at: {type: Date, default: Date.now()},
updated_at: {type: Date, default: Date.now()},
created_at_local_time: {type: String, required: true},
updated_at_local_time: {type: String, require: true},
}, {versionKey: false});
module.exports = mongoose.model('orders', orders);
运行上面的查询后,它会给我以下结果:
0|app | TypeError: Cannot read property 'startTransaction' of null
我正在使用猫鼬版本5.2.16
和mongodb版本4.0.5