Sequelize.js Sequelize:InstanceMethods不是';如果在.save()之前调用,则无法工作

Sequelize.js Sequelize:InstanceMethods不是';如果在.save()之前调用,则无法工作,sequelize.js,Sequelize.js,我已经试着解释了一段时间了 我有一个这样的续集模型: return sequelize .define('Post', { title: {type : DataTypes.STRING, allowNull: false, unique: false}, subtitle: {type : DataTypes.STRING, allowNull: true}, body: {type : DataTypes.STRING, al

我已经试着解释了一段时间了

我有一个这样的续集模型:

return sequelize
    .define('Post', 
    {
        title: {type : DataTypes.STRING, allowNull: false, unique: false},
        subtitle: {type : DataTypes.STRING, allowNull: true},
        body: {type : DataTypes.STRING, allowNull: false},
        status: {type : DataTypes.STRING, allowNull: false, defaultValue: "draft"},
        slug: {type : DataTypes.STRING, allowNull: false, unique: true, defaultValue: "slug"},

        created_at: {type : DataTypes.DATE, allowNull: false, defaultValue: new Date()},
        updated_at: {type : DataTypes.DATE, allowNull: false, defaultValue: new Date()},
        published_at: {type : DataTypes.DATE, allowNull: true, defaultValue: null}
    },  {
        associate: function(models){
            models.Post.hasMany(models.Tag);
            models.Post.hasMany(models.Category);
        },
        instanceMethods: {
            generateSlug: function(link){
                this.slug = _s.slugify(link);
                console.log('New Slug: '+this.slug);
                return;
            },
                  }
我用以下方法创建了一个新帖子:

'createPost': function(req, res, next){ 

        db.Post.build({
            title:          req.body.post.title,
            subtitle:       req.body.post.subtitle,
            body:           req.body.post.body,
        }).generateSlug(req.body.post.title).save()
        .success(function(post){
            console.log('Successfully created post: '+post.title);
            res.json({post: post});
        }).failure(function(err){
            console.log('Error in creating post');
            res.send(404);
        });
},
这不起作用,对save()的调用也不起作用,尽管代码确实流入generateSlug,但对This.slug的调用会产生默认值,而不是标题slagified

如果我对其进行更改,以便在保存模型后调用generateSlug instanceMethod,则它可以完美地工作:

db.Post.build({
            title:          req.body.post.title,
            subtitle:       req.body.post.subtitle,
            body:           req.body.post.body,
        }).save()
        .success(function(post){
                            post.generateSlug(post.title);
            console.log('Successfully created post: '+post.title);
            res.json({post: post});
        }).failure(function(err){...
有人能解释一下这是怎么回事吗?它与建议第一次尝试正确的db.build({}).instanceMethod().save()的文档不同。我做错什么了吗


非常感谢

你的generateSlug方法应该
返回这个
,这样你就可以在1月1日调用save了,就是这样。