Sails.js 通过id查找错误javascript
Sails.js Find where error javascript by id
我只想显示一个student.but有一个错误告诉我那不是一个函数
当前代码
detail: async function (req, res) {
var thatEvent = await Event.findOne(req.params.eventid);
if (!thatEvent) return res.notFound();
var event = await Event.findOne(req.params.eventid).populate(
"participants"
);
if (!event) return res.notFound();
var participants = await event.participants;
return res.json(participants);
},
这是当前代码的结果
[
{
"createdAt": 1636793903089,
"updatedAt": 1636793903089,
"id": 3,
"role": "student",
"username": "student1"
},
{
"createdAt": 1636793903089,
"updatedAt": 1636793903089,
"id": 4,
"role": "student",
"username": "student2"
},
{
"createdAt": 1636793903089,
"updatedAt": 1636793903089,
"id": 5,
"role": "student",
"username": "student3"
}
]
修改后
detail: async function (req, res) {
var thatEvent = await Event.findOne(req.params.eventid);
if (!thatEvent) return res.notFound();
var event = await Event.findOne(req.params.eventid).populate(
"participants"
);
if (!event) return res.notFound();
var participants = await event.participants.find({id:3});
return res.json(participants);
},
然后会显示不是函数的错误
error: Sending 500 ("Server Error") response:
TypeError: #<Object> is not a function
at Array.find (<anonymous>)
at Object.detail [as event/detail]
下面是我编辑的代码
var participants = await event.participants.find({id:3});
我参考了这里的网站来修改我的代码。现在结果正确了。
https://www.codegrepper.com/code-examples/javascript/array.find+find+is+not+a+function
var participants = await event.participants.find(({ id }) => id === 3);
结果
{
"createdAt": 1636793903089,
"updatedAt": 1636793903089,
"id": 3,
"role": "student",
"username": "student1"
}
我只想显示一个student.but有一个错误告诉我那不是一个函数
当前代码
detail: async function (req, res) {
var thatEvent = await Event.findOne(req.params.eventid);
if (!thatEvent) return res.notFound();
var event = await Event.findOne(req.params.eventid).populate(
"participants"
);
if (!event) return res.notFound();
var participants = await event.participants;
return res.json(participants);
},
这是当前代码的结果
[
{
"createdAt": 1636793903089,
"updatedAt": 1636793903089,
"id": 3,
"role": "student",
"username": "student1"
},
{
"createdAt": 1636793903089,
"updatedAt": 1636793903089,
"id": 4,
"role": "student",
"username": "student2"
},
{
"createdAt": 1636793903089,
"updatedAt": 1636793903089,
"id": 5,
"role": "student",
"username": "student3"
}
]
修改后
detail: async function (req, res) {
var thatEvent = await Event.findOne(req.params.eventid);
if (!thatEvent) return res.notFound();
var event = await Event.findOne(req.params.eventid).populate(
"participants"
);
if (!event) return res.notFound();
var participants = await event.participants.find({id:3});
return res.json(participants);
},
然后会显示不是函数的错误
error: Sending 500 ("Server Error") response:
TypeError: #<Object> is not a function
at Array.find (<anonymous>)
at Object.detail [as event/detail]
下面是我编辑的代码
var participants = await event.participants.find({id:3});
我参考了这里的网站来修改我的代码。现在结果正确了。
https://www.codegrepper.com/code-examples/javascript/array.find+find+is+not+a+function
var participants = await event.participants.find(({ id }) => id === 3);
结果
{
"createdAt": 1636793903089,
"updatedAt": 1636793903089,
"id": 3,
"role": "student",
"username": "student1"
}