javascript - 用mongoose Model.create(doc,cb)无效

查看:90
本文介绍了javascript - 用mongoose Model.create(doc,cb)无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

如题,跟踪进不了回调,在sechma的pre save hook能跟踪到确实执行到了pre save,但是没有保存到数据库。新添加了post save hook 但log中没有输出,说明save没执行完成。

  1. sechma

var mySchema = new mongoose.Schema({
    employee_name: { type: String },
    employee_no: { type: String },
    month: { type: Number },
    date: [String],
    lastModifyDate: Date
});
mySchema .pre("save", function (next) {    
    this.lastModifyDate = Date.now();
    console.log("save one:", this);
});

  1. 业务层

var createPromise = function (doc,newData) {
    return new Promise(function (fulfill, reject) {
    ...省略doc的校验 doc为上个promise传入的查询结果
    myModel.create({
                    employee_name: newData.emp_name,
                    employee_no: newData.emp_no,
                    month: newData.month,
                    overtime_date: newData.addDates.rmArr(newData.rmDates)
                }, function (err, res) {
                    if (err) return reject(err);
                    return fulfill(res);
                });
});
createPromise
.then(function(res){...})
.catch(...)

现在跟踪进入到create后没有执行then或者catch中的console.log

解决方案

mySchema .pre("save", function (next) {    
    this.lastModifyDate = Date.now();
    console.log("save one:", this);
    next();
});

这篇关于javascript - 用mongoose Model.create(doc,cb)无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆