2015-04-23 49 views
1

在我sailsjs的應用程序,我想添加一個默認的頭像我的用戶在創建後的用戶,所以我必須在此models/User.js鉤afterCreate:更新在sailsjs模型未定義afterCreate勾

var User = { 
    // ... 
    attributes: { 
    avatar: { model: 'Image' }, 
    // ... 
    }, 
    afterCreate: function(user, next){ 
    var url = sails.config.s3 + "/" + sails.config.default_avatar; 
    sails.log.info(user); // This displays the proper user 
    var data = { url: url, isAvatar: true, user: user.id }; 

    Image.create(data) 
    .exec(function (err, image) { 
     sails.log.info(image); // This displays the proper image 
     User.update(image.user, { avatar: image.id}) 
     .exec(sails.log.info); 
    }); 
    next(); 
    }, 
} 

我一直收到錯誤:

/Users/rcanty/Workspace/project/server/api/models/User.js:55 
    return User.update(image.user, { avatar: image.id}).exec(sails.log.info 
      ^
TypeError: undefined is not a function 
    at /Users/rcanty/Workspace/project/server/api/models/User.js:55:19 
at bound (/usr/local/lib/node_modules/sails/node_modules/lodash/dist/lodash.js:957:21) 
at applyInOriginalCtx (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:416:80) 
at wrappedCallback (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:315:18) 
at callback.success (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:33:31) 
at _switch (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:48:28) 
at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:241:9 
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:157:25 
at bound.<anonymous> (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/schema.js:151:44) 
at fn (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/callbacksRunner.js:60:10) 
at iterate (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:149:13) 
at Object.async.eachSeries (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:165:9) 
at Object.runner.afterCreate (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/callbacksRunner.js:63:9) 
at after (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:236:17) 
at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:223:67 
at bound (/usr/local/lib/node_modules/sails/node_modules/lodash/dist/lodash.js:957:21) 

但是它好像我做什麼公司在waterline docs

+0

你創建這個還是你帶帆產生的呢?我沒有看到你在哪裏出口這個 – tpie

+0

我在sails-auth-generate – onetwopunch

回答

2

規定,我認爲這種類型的事情壽ld在你的用戶控制器中,而不在模型內部。您試圖修改您正在定義的模型內定義的模型。

將它移動到控制器並在createUser函數結束時調用它。

編輯:

嘗試移動功能之外對象的屬性,並添加var User = this;,使其可以參考自己:

var User = { 
    // ... 
    attributes: { 
     avatar: {model: 'Image'}, 
     // ... 
    }, 
    afterCreate: function(user, next){ 
     var url = sails.config.s3 + "/" + sails.config.default_avatar; 
     sails.log.info(user); // This displays the proper user 
     var data = { url: url, isAvatar: true, user: user.id }; 
     var User = this; // for internal reference 

     Image.create(data) 
      .exec(function (err, image) { 
      sails.log.info(image); // This displays the proper image 
      User.update(image.user, { avatar: image.id}) 
       .exec(sails.log.info); 
      }); 
     next(); 
    } 
    } 
+1

生成了這個方法之後,把這個方法添加到了用戶類中我實際上不同意這個設計實踐,儘管今天我會試試看它是否它作品。如果我無法在afterCreate回調中向用戶添加默認圖像,那麼哪個IMO應該在哪裏,這似乎是一個相當大的Sails設計缺陷,因爲您可以在Rails的一行中執行此操作。 – onetwopunch

+0

Well..hang讓我看看那臺發電機......我不熟悉它給你帶來什麼。我正在使用的auth軟件包並不完全一樣。 – tpie

+0

好吧,試試這個......這就是它在我的模型中的設置。 – tpie