javascript - Update undefined in sailsjs model afterCreate hook -


in sailsjs app, i'm trying add default avatar user after user created have aftercreate hook in models/user.js:

var user = {   // ...   attributes: {     avatar: { model: 'image' },     // ...   },   aftercreate: function(user, next){     var url =  sails.config.s3 + "/" + sails.config.default_avatar;     sails.log.info(user); // displays proper user     var data = { url: url, isavatar: true, user: user.id };      image.create(data)      .exec( function (err, image) {       sails.log.info(image); // displays proper image       user.update(image.user, { avatar: image.id})       .exec( sails.log.info );     });     next();   },    } 

i keep getting error:

 /users/rcanty/workspace/project/server/api/models/user.js:55   return user.update(image.user, { avatar: image.id}).exec( sails.log.info               ^ typeerror: undefined not function     @ /users/rcanty/workspace/project/server/api/models/user.js:55:19    @ bound (/usr/local/lib/node_modules/sails/node_modules/lodash/dist/lodash.js:957:21) @ applyinoriginalctx (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:416:80) @ wrappedcallback (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:315:18) @ callback.success (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:33:31) @ _switch (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:48:28) @ /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:241:9 @ /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:157:25 @ bound.<anonymous> (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/schema.js:151:44) @ fn (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/callbacksrunner.js:60:10) @ iterate (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:149:13) @ object.async.eachseries (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:165:9) @ object.runner.aftercreate (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/callbacksrunner.js:63:9) @ after (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:236:17) @ /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:223:67 @ bound (/usr/local/lib/node_modules/sails/node_modules/lodash/dist/lodash.js:957:21) 

however seems i'm doing what's prescribed in waterline docs

i think type of thing should inside user controller, not inside model. trying modify model defining inside model defining.

move controller , call @ end of createuser function.

edit:

try moving function outside attributes object, , add var user = this;, can reference itself:

  var user = {     // ...     attributes: {       avatar: {model: 'image'},       // ...     },     aftercreate: function(user, next){       var url =  sails.config.s3 + "/" + sails.config.default_avatar;       sails.log.info(user); // displays proper user       var data = { url: url, isavatar: true, user: user.id };       var user = this; // internal reference        image.create(data)           .exec( function (err, image) {             sails.log.info(image); // displays proper image             user.update(image.user, { avatar: image.id})                 .exec( sails.log.info );           });       next();     }   } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -