[英]array.find() is returning undefined
I am trying to update a given subdocument inside an array using its id but when i use the vanila javascript array.find() i get undefined.我正在尝试使用其 id 更新数组内的给定子文档,但是当我使用 vanila javascript array.find() 时,我得到了未定义。 below is the code
下面是代码
let offers = service.offers
console.log("OFFERS ARE ==========", offers)
const offerIDObject = mongoose.Types.ObjectId(offerID)
console.log("CONVERTED ID IS ==========", offerIDObject)
// Find returns undefined for some reason
const offer = offers.find((el) => {
return el._id === offerIDObject
})
offers.map((el, i) =>console.log("ARE OF SAME TYPES======", typeof(el._id) === typeof(offerIDObject)))
console.log("THE FOUND OFFER IS=======",offer)
What could be the problem with the find method? find 方法可能有什么问题?
objectIds are never the same. objectIds 永远不会相同。 you can convert it to string, then compare them like this:
return el._id.toString() === offerIDObject.toString()
or use mongoose.equals() method:您可以将其转换为字符串,然后像这样比较它们:
return el._id.toString() === offerIDObject.toString()
或使用 mongoose.equals() 方法:
const offer = offers.find((el) => {
return el._id.equals(offerIDObject)
})
both should work.两者都应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.