Access request headers from beforeSave Model Hook

后端 未结 5 877
执笔经年
执笔经年 2021-01-18 05:07

How can i access the details of the user who raise the request from a Model Hook

Comment.beforeSave =  function(next,com) {
//Want to add 2 more properties          


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 05:43

    It seems that you can't update related model with simply overriding ctx.req.body. Instead of you should override ctx.args.data - it looks like this ctx parameter is used to initalize the related model.

    So it will look like that:

    MainItem.beforeRemote('**', function(ctx, user, next) {   
      if(ctx.methodString == 'leave_request.prototype.__create__comments'){  
         ctx.args.data.added_by = ctx.req.accessToken.userId;     
         ctx.args.data.added_at = new Date();                         
         console.log("Added headers as .."+ctx.args.data.added_by);
      }    
      else{  ... }
      next();
    

提交回复
热议问题