2016-11-30 25 views
0

我新手環回,我寫了一個功能註冊和 上beforeRemote執行。在這裏我可以訪問環回上下文。但用戶創建後,我執行另一個功能,使用afterRemote發送驗證鏈接,此處loopback上下文返回空值。迴環背景下返回null在afterRemote方法

function signup(){ 
    const lbCtx = LoopBackContext.getCurrentContext; 
    const details = lbctx().get('details'); 
    //here it's working 
} 

function sendVerificationEmail(){ 
    const lbCtx = LoopBackContext.getCurrentContext; 
    const details = lbctx().get('details'); 
    //here not working 
} 

Users.beforeRemote('create', (ctx) => signup(ctx)); 
Users.afterRemote('create', (ctx) => sendVerificationEmail(ctx)); 

任何人都可以幫助我實現這一點。

回答

0

a documented issue與在LoopBack中處理當前上下文的方式。

不幸的是,他們用來支持此功能的庫名爲continuation-local-storage似乎不是完全可靠的,有時上下文沒有正確傳遞,因此您得到的是空值。

There's a warning in the docs聲明這一點,並鏈接到不同的解決方法,因爲還沒有統一的解決方案。

注意:我在代碼中看到的一個問題(儘管可能是拼寫錯誤)是在signup()方法中,您正在執行get()而不是set()。以防萬一你沒有注意到。