0

I've got a problem. I'm creating a question to MongoDB:

const team: TeamDocument = await Team.findOne({ members: payload.id, name })

The problem is, members is an array of objects:

members: [{
    name: { type: Schema.Types.ObjectId, ref: 'User' },
    role: String
}]

So when I create ask, it showes an error:

UnhandledPromiseRejectionWarning: ObjectParameterError: Parameter "obj" to Document() must be an object, got 5e88984b1478de32f43289e8

How can I repair this, so it will check if the members array has an object which contains an id provided in payload?

2 Answers 2

1

If you have a single query you don't even need to use $elemMatch, you can just use:

const team: TeamDocument = await Team.findOne({ "members.name": payload.id })
Sign up to request clarification or add additional context in comments.

Comments

1

const team: TeamDocument = await Team.findOne({ members: { id: payload.id, name:name } })

Check This Official Mongo Documentation; https://docs.mongodb.com/manual/tutorial/query-array-of-documents/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.