为什么在mongoose中使用find方法时console.log返回空数组?

编程入门 行业动态 更新时间:2024-10-09 06:24:59

为什么在mongoose中使用find方法时console.log返回空<a href=https://www.elefans.com/category/jswz/34/1771288.html style=数组?"/>

为什么在mongoose中使用find方法时console.log返回空数组?

我是后端开发人员的新手。我正在尝试检查集合中是否已有用户,如果没有,则控制台日志为“ no”。我在'const b'中没有指示的数据,但是我一直在终端中获取空数组,而不是'no'。

const b = {email: "[email protected]", password: "wwww"}
MyModel.find(b)
    .then(exUs =>{
        if(exUs){
            console.log(exUs)
        } else {
            console.log("no")
        }          
    })
回答如下:

find总是返回一个cursor(如果没有文档,则可以为空[]),因此您的if条件将始终为true,并且如果文档为零,它将打印空数组[](永远不会返回游标空值)。

更多推荐

为什么在mongoose中使用find方法时console.log返回空数组?

本文发布于:2024-05-07 11:08:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1755961.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   方法   find   mongoose   console

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!