[英]Why does my Array.prototype.find() return undefined?
I have a route handler in which I am attempting to find a user's collection of club objects, then find the specific club object using Array.prototype.find().我有一个路由处理程序,我试图在其中找到用户的俱乐部对象集合,然后使用 Array.prototype.find() 找到特定的俱乐部 object。 I have no idea why it is always returning undefined.
我不知道为什么它总是返回未定义。 Can anyone see what I am doing wrong?
谁能看到我做错了什么?
router.get('/club', async (req,res) => {
try {
console.log(req.query.club);
// 6008d7537ea5c22b61dc616b
const { clubs } = await User.findOne({ spotifyId: req.query.id }).populate('clubs').select('clubs');
console.log(clubs);
// [{"_id":"6008d7537ea5c22b61dc616b","name":"asfasfs","description":"sdfasfa","privacy":"private","frequency":"bi-weekly","__v":0},{"_id":"6008ec8026900630c24fd533","name":"asfasfdsasf","description":"asdfasfdsf","privacy":"private","frequency":"monthly","__v":0}]
let club = clubs.find(currentClub => (
currentClub._id === req.query.club
));
console.log(club)
// undefined
res.send(club)
}
catch {
return res.status(400).send('No club found with given id')
}
})
_id is an object _id是object
Change your code like this像这样更改您的代码
String(currentClub._id) === req.query.club
or或者
currentClub._id.toString() === req.query.club
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.