与Pre Hook无关的猫鼬节约计划

时间:2017-12-11 06:30:19

标签: node.js mongodb mongoose mongoose-schema

我是Nodejs和Mongoose的新手,事情一直很好,直到新版本的mongoose承诺贬值。我看到建议使用我想做的蓝鸟。我有以下代码:

app.post('/users', (req, res) => {
var body = _.pick(req.body, ['email', 'password']);
var user = new User(body);
user.save().then(() => {
    return user.generateAuthToken();
}).then((token) => {
    res.header('x-auth', token).send(user);
}).catch((e) => {
    res.status(400).send(e);
})

});

和预钩:

UserSchema.pre('save', function (next) {
var user = this;
if (user.isModified('password')) {
    bcrypt.genSalt(10, (err, salt) => {
        bcrypt.hash(user.password, salt, (err, hash) => {
            user.password = hash;
            next();
        });
    });
} else {
    next();
}

});

创建哈希并调用next()后,为什么会发生这种情况? 所有帮助将不胜感激

编辑: 呼叫永远不会进入generateAuthToken()功能

2 个答案:

答案 0 :(得分:0)

试试这个

protected void Unnamed_Click(object sender, EventArgs e)
    {
        lbl.Text = "Retriving Employee details";
        GetEmplyeeDetails();
        lbl.Text = "Processing Employee details";
        ProcessEmployeDetails();
        lbl.Text = "Generating reports";
        GenerateReports();
        lbl.Text = "Uploading...";
        UploadDetails();
        lbl.Text = "Pushing data to database";
        PushResultToDatabase();

        lbl.Text = "Completed Successfully";
    }

    private void GetEmplyeeDetails()
    {

    }
    private void ProcessEmployeDetails()
    {

    }

    private void GenerateReports()
    {

    }

    private void UploadDetails()
    {

    }

    private void PushResultToDatabase()
    {

    }

答案 1 :(得分:0)

我发现我导入的mongoose变量不正确,我通过修复它解决了这个问题。