2016-01-03 58 views
0

如何使用apigee-access設置變量用於表達?如何在節點js中使用apigee-access設置變量?

我想用這樣的apigee訪問得到一個變量:

http.createServer(function(req, resp) { 
    var userId= apigee.getVariable(req,username); 
    resp.end('Hello, World!\n'+userId+' Error:'+err);  
    }); 

並嘗試使用該變量在userId的

app.post('/employees', function(req, res) { 
if (!req.is('json')) { 
    res.jsonp(400, { 
     error : 'Bad request' 
    }); 
    return; 
} 



var b = req.body; 
var e = { 
    'userName' : userId, 

    'displayName' : userId+"_details", 
    'phone' : b.phone 
}; 

createEmployee(e, req, res); 

});

我收到一個錯誤ReferenceError:" userId "未定義。同時執行相同的操作。有沒有辦法訪問這個變量?

回答

0

您已將userId定義爲createServer函數範圍內的局部變量。在服務器回調被觸發並且userId被定義的時候,你的e setter代碼已經被執行了。

我想它在resp.end工程雖然正確?它將它寫入頁面?你需要設計一個更類似於節點的方式來處理userId,它不需要將userId存儲爲全局變量(這將非常糟糕),甚至可以將其存儲在代碼中。我建議使用路徑路徑並要求最終用戶在製作POST時指定userId:

app.post('/employees/:userId', function(req, res) { 
    // req.params.userId can now be used 
    if (!req.is('json')) { 
     res.jsonp(400, { 
      error: 'Bad request' 
     }); 
     return; 
    } 

    var body = { 
     userName: req.params.userId, 
     displayName: req.params.userId+"_details", 
     phone: req.body.phone 
    }; 

    // now you're probably going to do something with 'body' 
}); 
相關問題