Mongoose type "accessibleBy" returns an array of document after findOne
See original GitHub issueModel.accessibleBy
from @casl/mongoose
returns an array of document which makes it hard to deal with with operations such as findById
and following:
type CatDocument = Cat & Document
getById(id: MongooseTypes.ObjectId, ability: Ability): Promise<CatDocument>
return this.catModel
.findById(id)
.accessibleBy(ability, Action.Read)
// function return type now needs to be Promise<CatDocument[]>
.exec()
}
Update operations after find one (is there a better way to check ability before update?) trigger an error since it expects an array:
async update(payload: UpdateCatInput, ability: Ability) {
const doc = await this.catModel
.findById(payload.id)
.accessibleBy(ability, Action.Update)
.exec()
if (doc) return doc.set(payload).save()
// Error : Property 'set' does not exist on type '(Cat & Document<any, any, any> & { _id: ObjectId; })[]'
}
Issue Analytics
- State:
- Created a year ago
- Comments:21 (8 by maintainers)
Top Results From Across the Web
Mongoose findOne returns empty array field - Stack Overflow
With Mongoose, you have to populate candyTypes array: ... As an aside, your document contains a field price but your schema names it...
Read more >06 - Use model.findOne() to Return a Single Document
06 - Use model. findOne () to Return a Single Document - MongoDB and Mongoose - freeCodeCamp Tutorial. 8K views 2 years ago....
Read more >Mongoose v6.8.2: API docs
Returns true if the given value is a Mongoose ObjectId (using instanceof ) or if ... document from the database using findOne() or...
Read more >Use mongoose to find in an Array of Objects | ObjectRocket
This tutorial will show you how to use mongoose to find in an array of objects.
Read more >How to avoid accidentally returning an arbitrary document ...
When you use findOne to return a single document, but the specified field in the filter does not exist (for example because of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I opened an issue at mongoose https://github.com/Automattic/mongoose/issues/12286
As a workaround you can use any
findOne*
method sincefindById
is just an alias.find().findByIdAndDelete()
is not found butfind().findOneAndDelete()
is. I would advice not using thesefindById
shorthands as they often create problems.