0
我使用CLI將動作部署到OpenWhisk作爲Web動作。我試圖從郵差/捲曲中調用它。我回來,我想在身體參數的建議 hereOpenWhisk WebAction響應返回空體
這裏的響應是我的代碼:
function login(args){
const username = args.user.username;
const password = args.user.password;
const user = {
uName: 'abc',
userCn: 'Full Name',
token: 'fsgdfhj'
};
function authenticateUser(){
if(username === '' || password === ''){
return new Promise((resolve, reject) => {
reject({
headers: { 'Content-Type': 'application/json' },
statusCode: 401,
body: "Please enter username and password!"
});
});
}
else if(username==='a' && password ==='a'){
return new Promise((resolve, reject) => {
resolve({
headers: { 'Content-Type': 'application/json' },
statusCode: 200,
body : user
}) ;
});
}
}
authenticateUser()
.then(() => {
console.log('resolved and body is -> '+user);
})
.catch(()=> {
console.log('rejected -> '+stderr);
});
}
exports.main = login;
部署行動:
$ wsk -i action create /guest/package/actionName --kind nodejs:6 zipFileWithPackageJson.zip --web true
的情況是,當我使用if-else if
循環沒有把它包含在function authenticationUser()
中,我可以得到響應。當我把它放在函數中並嘗試像上面這樣調用函數時,我得到一個空白的響應。 我必須將它放在一個函數中,因爲在檢查用戶名和密碼之前,我必須執行一系列操作。我會在不同的功能封裝這些操作並使用
function1()
.then(function2)
.then(function3)
.then(authenticateUser)
.catch(err => {
console.log(err)
});
調用它們一個接一個。如果我使用命令$ wsk -i activation logs <activation ID>
檢查日誌這個動作我可以看到他們預期。只是響應主體完全是空的。
它是NodeJS中的語法,我寫錯了,還是OpenWhisk,直接在主函數內部的決心塊?
啊!這樣一個小錯誤!謝謝你的幫助。 –