0

I pass ids in a post request in postman and reference to another model and get

5dcf8dc320742961c8b5a801, 5dcf8eca20742961c8b5a808, 5dcf8c8fef739a6159938ca8

in my controller I convert them as

let ids = [req.body.ids]
console.log(ids)

and get

['5dcf8dc320742961c8b5a801, 5dcf8eca20742961c8b5a808, 5dcf8c8fef739a6159938ca8']

I wanted to convert them to an ObjectId so I tried

ObjectId(numberArray)

and

numberArray.map(num => ObjectId(num))

but all return

(node:25080) UnhandledPromiseRejectionWarning: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
2

1 Answer 1

0

I got it, I mean someone suggest me to use split that returns a new array

let singers = ['5dcf8dc320742961c8b5a801, 5dcf8eca20742961c8b5a808, 5dcf8c8fef739a6159938ca8']
let singerArr = singers.split(', ')
console.log(singerArr);
[ '5dcf8c8fef739a6159938ca8',
  '5dcf8dc320742961c8b5a801',
  '5dcf8eca20742961c8b5a808' ]

and use for loop and find query to match to these ids and use mongoose.Types.ObjectId

though I'm having a different scenario to fix right now

Sign up to request clarification or add additional context in comments.

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.