簡體   English   中英

檢查兩個變量是否未定義

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM