skip to Main Content

I can’t figure out why mongoose can’t complete this Model.create.

same connection works fine with other controller functions

vscode

postman

i am trying to create a new document but my code just gets stuck

2

Answers


  1. I would suggest handling the Promise so you can try and figure out what is going on.

    Login or Signup to reply.
  2. 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

    const createNote = asyncHandler(async (req, res)=>{
        try{
    
            // your code here
    
        } catch(e){
    
            console.error("error handling note creation: ", e);
            res.status(500).send()
    
        }
    
    })
    
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search