Javascript 请帮助,我有一个问题,我的条件不适用于我从mongoDB.find()获得的数据
我有一个问题,我使用javascript编写代码,我使用mongodb或mongoose作为数据库。我有一些关于async/Wait的信息 我使用const-thread=await-Threads.findOne({threadId})创建了一个名为“thread”的变量来从mongoDB获取数据 线程将具有以下数据:{“_id”:“6044d0d8940c3b2494ce135d”,“threadId”:“A001} 之后,我想使用此“线程”中的_id检查标记为Post的用户,如果用户标记为Post的线程id与线程中的_id相同,我想使用splice将其删除 标记为POST的用户将具有数据,例如:{“threadId”:“6044d0d8940c3b2494ce135d”,“threadId”:“6044d0e0940c3b2494ce135e”} 但问题是条件代码“if(markpost[i].threadId==thread.\u id)”,它不起作用,即使markpost[i].threadId与thread相同,它也不做任何事情。\u id,它不做console.log(“find ya”) 那么你能帮我解决这个问题吗?下面是我的代码,我还对不起作用的部分进行了评论Javascript 请帮助,我有一个问题,我的条件不适用于我从mongoDB.find()获得的数据,javascript,mongodb,async-await,Javascript,Mongodb,Async Await,我有一个问题,我使用javascript编写代码,我使用mongodb或mongoose作为数据库。我有一些关于async/Wait的信息 我使用const-thread=await-Threads.findOne({threadId})创建了一个名为“thread”的变量来从mongoDB获取数据 线程将具有以下数据:{“_id”:“6044d0d8940c3b2494ce135d”,“threadId”:“A001} 之后,我想使用此“线程”中的_id检查标记为Post的用户,如果用户标记为
const thread = await Threads.findOne({threadId})
const users = await Users.find({"markedPost.threadId":{
$in:[thread._id]
}})
if(users.length > 0){
await users.map(async user=>{
const markpost = user.markedPost;
let index = 0;
for (let i = 0; i < markpost.length; i++) {
console.log(markpost[i].threadId)
console.log(thread._id)
if(markpost[i].threadId === thread._id){
//this if didnt work at all, I dont know why, even when the threadId is the same with thread._id
console.log("found ya")
index = i;
break
}
console.log(markpost[i])
}
markpost.splice(index,1);
await Users.findOneAndUpdate({
userId:user.userId
},{
markedPost:[...markpost]
})
})
}
编辑:我的错误在这里,线程.u id类型是ObjectId,而另一个是string,因此它将返回false
感谢毗瑟奴纠正了我的错误是
markpost[i]的类型。threadId
和thread.\u id
相同?。因为你使用的是====
操作符,所以两者的类型应该是相同的。例如:ifthread.\u id
是ObjectId
和markpost[i]类型.threadId
的类型是String
它返回false
哦,我的天哪,从你们的评论中意识到我的错误。他们两个都不是同一类型的,我原以为都是String,但线程。_id是ObjectId,而另一个是String谢谢你们,伙计,你们真的帮了我
markpost[i].threadId: 6044d0e0940c3b2494ce135e
thread._id : 6044d0e0940c3b2494ce135e
{
threadId: '6044d0e0940c3b2494ce135e',
date: 2021-03-07T13:15:45.713Z
}
markpost[i].threadId: 6044d0d8940c3b2494ce135d
thread._id : 6044d0e0940c3b2494ce135e
{
threadId: '6044d0d8940c3b2494ce135d',
date: 2021-03-07T13:15:47.967Z
}