So I can’t see your schema for Note, but I assume that the user field is supposed to have the type Ref<User>, which is to say a reference to a User model that you have defined elsewhere. But in the sample of code you provided, it would appear you gave it a mongoID string.
If you add the following line the script should start working. This converts your user variable from a string representation of an mongo ObjectID to a true mongo ObjectID. :
user = new mongoose.Types.ObjectId(user);
However, I think your main problem is actually just that you didn’t get an error print out. My guess is that the asyncHandler made the error disappear. If you put the body of your function inside a try catch block with a console.error in the catch, you should be able to see that error in the future
This is mostly guesswork since I don’t have a lot of context. If this doesn’t solve it, please post what the schema is for your Note model and any error printout you get.
2
Answers
I would suggest handling the Promise so you can try and figure out what is going on.
So I can’t see your schema for Note, but I assume that the
user
field is supposed to have the typeRef<User>
, which is to say a reference to aUser
model that you have defined elsewhere. But in the sample of code you provided, it would appear you gave it a mongoID string.If you add the following line the script should start working. This converts your user variable from a string representation of an mongo ObjectID to a true mongo ObjectID. :
user = new mongoose.Types.ObjectId(user);
However, I think your main problem is actually just that you didn’t get an error print out. My guess is that the
asyncHandler
made the error disappear. If you put the body of your function inside a try catch block with a console.error in the catch, you should be able to see that error in the futureThis is mostly guesswork since I don’t have a lot of context. If this doesn’t solve it, please post what the schema is for your
Note
model and any error printout you get.