如何从“访问”钩子环回访问'req'对象?

时间:2022-06-28 19:02:20

It seems operation hook "access" does not contains ctx.req object.

似乎操作钩子“访问”不包含ctx.req对象。

What i am trying to achieve is that session data should be available in all the models.

我想要实现的是会话数据应该在所有模型中都可用。

Session defined in middleware:

中间件中定义的会话:

 "session": {
     "express-session": {
         "params": {
         "secret": "mysceret",
         "saveUninitialized": true,
         "resave": true
        }
    }
 }

In User.js :

在User.js中:

req.session.user = userData; 

and to access session in Post model:

并在Post模型中访问会话:

Post.observe('access', function(ctx, next) {     
   console.log('ctx.req : ' , ctx.req) // undefined
   ctx.query.filter = { tenantId: ctx.req.session.user.tenantId }; 
   // so cannot able to find session data here.   
   next();
}); 

Express-session : "express-session": "^1.15.6"

快递:“快递会”:“^ 1.15.6”

Loopback version : "loopback": "^3.0.0"

Loopback版本:“loopback”:“^ 3.0.0”

What I am missing or I am access session in a wrong way ? Please some help.

我错过了什么,或者我以错误的方式访问会话?请帮忙。

Thanks

谢谢

1 个解决方案

#1


0  

I'm going to heavily borrow from my answer at accessing current logged in user id in custom route in loopback

我将在环回中访问自定义路由中的当前登录用户ID时大量借用我的答案

The operation hook context accepts what values you submit to it, as well as a few defaults related to the model, it would never have the userId by default. https://loopback.io/doc/en/lb3/Operation-hooks.html#operation-hook-context-object

操作挂钩上下文接受您提交给它的值,以及与模型相关的一些默认值,默认情况下它永远不会有userId。 https://loopback.io/doc/en/lb3/Operation-hooks.html#operation-hook-context-object

The ctx in an operation hook is not the same as the ctx in a remote method.

操作挂钩中的ctx与远程方法中的ctx不同。

If you want to pass in additional operation hook context values then you must use call a model method manually and send them as the second parameter.

如果要传递其他操作挂钩上下文值,则必须手动调用模型方法并将其作为第二个参数发送。

model.create({color: 'red'}, {contextValue: 'contextValue', anotherContextValue: 'anotherContextValue'}, (err, obj) => {
   // Callback things...
}

#1


0  

I'm going to heavily borrow from my answer at accessing current logged in user id in custom route in loopback

我将在环回中访问自定义路由中的当前登录用户ID时大量借用我的答案

The operation hook context accepts what values you submit to it, as well as a few defaults related to the model, it would never have the userId by default. https://loopback.io/doc/en/lb3/Operation-hooks.html#operation-hook-context-object

操作挂钩上下文接受您提交给它的值,以及与模型相关的一些默认值,默认情况下它永远不会有userId。 https://loopback.io/doc/en/lb3/Operation-hooks.html#operation-hook-context-object

The ctx in an operation hook is not the same as the ctx in a remote method.

操作挂钩中的ctx与远程方法中的ctx不同。

If you want to pass in additional operation hook context values then you must use call a model method manually and send them as the second parameter.

如果要传递其他操作挂钩上下文值,则必须手动调用模型方法并将其作为第二个参数发送。

model.create({color: 'red'}, {contextValue: 'contextValue', anotherContextValue: 'anotherContextValue'}, (err, obj) => {
   // Callback things...
}