获取消息存在于控制台中而 UID 不在数据库中

发布于 2025-01-13 23:23:08 字数 470 浏览 1 评论 0原文

我正在尝试检查 UID 是否存在于数据库中。如果它存在,我希望它不写入数据,否则写入数据。

但由于某种原因,当没有这样的 UID 时,我就会存在。

输入图片此处描述

async function engagements(){
    // const db =  ;
    if (await Engagements.find({uid:"f60c7612-93f5-48b6-8039-94cf5668f6b4"})) {
        console.log("exist");
    }else{
        console.log("does not exist");
    }
}

I am trying to check if the UID exists in db. If it exist I want it not to write a data else write a data.

But for some reason I am getting exist when there is no such UID.

enter image description here

async function engagements(){
    // const db =  ;
    if (await Engagements.find({uid:"f60c7612-93f5-48b6-8039-94cf5668f6b4"})) {
        console.log("exist");
    }else{
        console.log("does not exist");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是偏爱你 2025-01-20 23:23:08

我找到了解决方案。
.find() 返回一个等于 true 的空数组的情况。
基本上我做了一个 const 并检查数组是否为空

const db =  await Engagements.find({uid:"f60c7612-93f5-48b6-8039-94cf5668f6b4"});
    if (db.length > 0) {
        console.log("exist");
    }else{
        console.log("does not exist");
    }

I found the solution.
The case that .find() returns an empty array which is equal to true.
Basically I made a const and checked the array if it is empty or not

const db =  await Engagements.find({uid:"f60c7612-93f5-48b6-8039-94cf5668f6b4"});
    if (db.length > 0) {
        console.log("exist");
    }else{
        console.log("does not exist");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文