[英]Check two variables for undefined
JS:
var ctoken = req.cookies.user;
var stoken = req.session.passport.user;
if(ctoken === 'undefined' || stoken === 'undefined'){
return res.send('invalid token');
}else{
if (ctoken.split('_')[0] !== stoken) {
return res.send('invalid token');
}
}
在
ctoken.split('_')[0]
拋出一個錯誤:
cannot call split of undefined.
為什么? 由於if
條件,不應發生這種情況。
刪除引號:
if (ctoken === undefined || stoken === undefined) {
也許您對某些程序員建議使用以下工具進行測試的趨勢感到困惑
if (typeof something === 'undefined') {
但是最好的測試是簡單地與undefined
比較。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.