2014-12-01 57 views
1

環回模式鉤內使用模型實例,我知道你可以訪問如User模型的實例是通過使用beforeCreate創建之前:SLC回送:從模型鉤

User.beforeCreate = function(next, userInstance) { 
    //your logic goes here using userInstance 
    next(); 
}; 

但如果我需要添加一些使用剛剛創建的用戶的firstName的應用程序邏輯,我該怎麼做?

User.afterCreate = function(next) { 
    //I need access to the user that was just created and some of it's properties 
    next(); 
}; 

有沒有辦法讓剛剛創建或者我需要改變使用我的應用程序邏輯,而不是之前和之後用戶的持有?

回答

1

您可以通過「這個」可以訪問更新/創建模型實例:

User.afterCreate = function(next) { 
    var user = this; 
    console.log("User created with id: " + user.id) 
    next(); 
};