question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mongoose type "accessibleBy" returns an array of document after findOne

See original GitHub issue

Model.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:closed
  • Created a year ago
  • Comments:21 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
gterrascommented, Aug 18, 2022

I opened an issue at mongoose https://github.com/Automattic/mongoose/issues/12286

As a workaround you can use any findOne*method since findById is just an alias.

1reaction
gterrascommented, Oct 10, 2022

find().findByIdAndDelete() is not found but find().findOneAndDelete() is. I would advice not using these findById shorthands as they often create problems.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found