我有一个使用Node.js,Express和Mongoose的后端。我正在尝试过滤嵌套在对象深处的文档数组,但是由于某种原因我无法访问该数组。路线> transform(df, Any_thrombosis = ifelse((Arterial_thrombosis == 'Yes') | (Venous_thrombosis == 'Yes'), 'Yes','No'))
Patient_number Arterial_thrombosis Venous_thrombosis Any_thrombosis
1 1 Yes No Yes
2 2 No Yes Yes
>
包含已登录用户的附件文档。以下是mycode。
req
如果我router.post("/users/:id/accept", auth, async (req, res) => {
try {
// this filter is not working
await req.user.friendRequests.filter((usr) => usr.owner !== req.params.id);
await req.user.save();
res.send("Accepted")
} catch (error) {
res.status(400).send(error.message);
}
});
会得到:
console.log(req.user)
如果我{
dateOfBirth: { date: 28, month: 'Sep', year: 2002 },
gender: 'male',
cover: null,
_id: 5f71c0674a54b03e70fcbe97,
firstName: 'jake',
lastName: 'sulli',
email: 'hello2@gmail.com',
friendRequests: [
_id: 5f71c1184a54b03e70fcbea0,
owner: '5f71c0d14a54b03e70fcbe9b',
friend: false,
name: 'mad max'
},
{
_id: 5f76a7872b21c8208f3caa5f,
owner: '5f742182931ab9125bc2ec9d',
friend: false,
name: 'shriya rai'
},
{
_id: 5f76f6fa85e74758e76cb1ec,
owner: '5f76f541ab7a1255dc78987d',
friend: false,
name: 'dara singh'
}
],
}
,则无法获得friendRequests
数组,但可以得到console.log()
,name
和email
。我要实现的是,如果gender
,那么我想从数组中删除该对象。
答案 0 :(得分:0)
您的friendRequests
数组存在一些问题。
首先,您没有正确开始或结束对象。
friendRequests: [
{ <=========================== Need another bracket
_id: 5f71c1184a54b03e70fcbea0,
owner: '5f71c0d14a54b03e70fcbe9b',
friend: false,
name: 'mad max'
},
{
_id: 5f76a7872b21c8208f3caa5f,
owner: '5f742182931ab9125bc2ec9d',
friend: false,
name: 'shriya rai'
},
{
_id: 5f76f6fa85e74758e76cb1ec,
owner: '5f76f541ab7a1255dc78987d',
friend: false,
name: 'dara singh'
}
]
第二,确保将_id转换为字符串
5f76f6fa85e74758e76cb1ec to "5f76f6fa85e74758e76cb1ec"