簡體   English   中英

貓鼬查找具有多個條件的查詢

[英]mongoose find query with multiple condition

我正在嘗試在注冊用戶時驗證驗證碼。 查詢有多個條件。 如果返回了文檔,則驗證碼將被驗證,否則不會被驗證。 來自以下代碼段的問題,盡管未返回文檔,但始終滿足if(foundUser)。

UserModel
        .find({
            email,
            verificationCode: code
        },
        (err, foundUser) => {
            if(err) {
                return res.json({msg: "Error"})
            }
            else {
                console.log('foundUser', foundUser)
                if(foundUser) {
                    return res.json({msg: "found email + code"})
                }
                else {
                    return res.json({msg: "No email & code matched"})
                }

            }
        })

foundUser可以用3種方式編寫

  if (foundUser.length !== 0) {}
  if (!foundUser.length) {}
  if (foundUser.length > 0) {}

選擇最適合您的一種。

我建議選擇2號

UserModel
            .find({
                email,
                verificationCode: code
            },
            (err, foundUser) => {
                if(err) {
                    return res.json({msg: "Error"})
                }
                else {
                    console.log('foundUser', foundUser)
                    if (!foundUser.length) {
                        return res.json({msg: "found email + code"})
                    }
                    else {
                        return res.json({msg: "No email & code matched"})
                    }

                }
            })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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