0

I have UserSchema and CreditSchema

const UserSchema = new Schema({
  credit: {
    type: Schema.Types.ObjectId,
    ref: 'Credit'
  }
})

const CreditSchema = new Schema({
  userId: Schema.Types.ObjectId,
  credit: {
    type: Number,
    default: 0
  }
})

Then I do these 2 action.

//save to credit
await new Credit({
  userId: mongoose.Types.ObjectId('5a3e76ce914e1d1bd854451d'),
  credit: 100
}).save()

The data been inserted successfully.

enter image description here

//then I try to populate credit
await User.find({}).populate('credit').exec()

I got error of

(node:17858) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): CastError: Cast to ObjectId failed for value "0" at path "_id" for model "Credit"

Why? I thought I already cast string to ObjectId when I was saving a new credit document?

Hoknimo
  • 533
  • 2
  • 6
  • 15
  • `"0"` is not a valid ObjectId value. Which is exactly what the error message says. That's what is coming from your `populate()` call? – Neil Lunn May 20 '18 at 08:26
  • `123` is not a valid objectid – Mạnh Quyết Nguyễn May 20 '18 at 08:27
  • @NeilLunn updated the error message I got in my terminal. – Hoknimo May 20 '18 at 08:29
  • @MạnhQuyếtNguyễn Yeah that does not help when people obfuscate `ObjectId` values in questions. But 's the `"0"` being read from the "credit" field in the document that is the actual error here. Which means the OP has actually been told this before. – Neil Lunn May 20 '18 at 08:29
  • @MạnhQuyếtNguyễn I put `123` as a placeholder now I used the real id, updated my question. – Hoknimo May 20 '18 at 08:29
  • This is the same question as the last one. With the same problem. You have invalid data stored. Show a document, but it would appear `0` is in the credit field instead of an `ObjectId` value like the schema is expecting. – Neil Lunn May 20 '18 at 08:30
  • Ok updated my document, it was inserted just now. – Hoknimo May 20 '18 at 08:32
  • That's the user data right? credit is supposed to be an `ObjectId`. Look at your schema. Show the `User` and not the `Credit`. Also the `Credit` has no `userid` value. This is what you were told on your last question, but it seems you did not bother to read the duplicate links. – Neil Lunn May 20 '18 at 08:33
  • @NeilLunn No, it's credit's document. – Hoknimo May 20 '18 at 08:34
  • @NeilLunn if you use .populate() there's no need to have credit_id in the User's document. I think you're confused with $lookup. – Hoknimo May 20 '18 at 08:36
  • Show a valid `User` document. This is exactly the same problem since you don't have matching data. If that's the `Credit` then you failed to load a `userid` value. And I suspect the `User` has the same problems. Read the links and understand that "joining" in any form requires valid references of the "same type" to be present in both sources. – Neil Lunn May 20 '18 at 08:36
  • @BhushanBabar it doesn't make any different. I got the same error still trying your code. – Hoknimo May 20 '18 at 08:39
  • @Hoknimo I'm not confused. That's how "joins" work. Read the [documentation for mongoose as well](http://mongoosejs.com/docs/populate.html). Fix up your data since you don't have anything "related" recorded anywhere. – Neil Lunn May 20 '18 at 08:40
  • @NeilLunn I just found that my old data messed up in User's document. You're right, I have a zero credit in User document. Thanks for your guide!! – Hoknimo May 20 '18 at 08:45
  • 1
    See also [Querying after populate in Mongoose](https://stackoverflow.com/q/11303294/2313887). Not only does it show there's no difference in how a "join" works, but there's also detail showing why exactly you preferably use `$lookup` now in most scenarios. – Neil Lunn May 20 '18 at 08:49

0 Answers0