Access request headers from beforeSave Model Hook

后端 未结 5 929
执笔经年
执笔经年 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条回答
  •  猫巷女王i
    2021-01-18 05:43

    The beforeRemote hooks execute before the model hooks, so you can add the userId to the request body.

    Comment.beforeRemote('**', function (ctx, unused, next) {
        var userId = ctx.req.accessToken.userId;
        if (ctx.methodString == 'Comment.create' || ctx.methodString == 'Comment.updateAttributes') {
            ctx.req.body.userId = userId;
        }
        next();
    });
    

    you might want to review which methodstring suit you best.

提交回复
热议问题