Reputation: 86
I created a schema.methods.toAuthJSON
which will return filtered data.
while i use findOne()
it works just fine. but When I use just find()
it doesn't work. how should i get it right?
Member.findOne().exec((err, member) => {
res.json(member.toAuthJSON())
})
Upvotes: 0
Views: 202
Reputation: 10899
The question definitely needs more detail but IF we assume as stated that the shown findOne
code works but the not shown find
code does not, then it is probably due to the return value not being used correctly for the find
code.
find
code:Member.find().exec((err, member) => {
res.json(member.toAuthJSON())
});
Again, if this assumption is correct then it is a misunderstanding of the returned value from find
. While findOne
will return a single document, find
returns an array of documents. So the assumed code would need to be updated to iterate over the returned array invoking the custom method for each document.
One approach would be to use a map:
Member.find().exec((err, members) => {
res.json(members.map((member) => member.toAuthJSON()));
});
This approach will map over the returned array of documents returning a new array of outputs from the custom method.
Upvotes: 1