Ohad
Ohad

Reputation: 13

Problem with promise handling using Mongoose

I'm working on my own project. His main idea is movie reviews.

I'm currently working on the HTTP requests trying to make my code a little better. I guess I have a problem with my promise handling:

    try {
        let newMovie = await MovieRepository.addNewMovie({
            name: req.body.name,
            year: req.body.year,
            description: req.body.description,
            length: req.body.length,
            genres: req.body.genres,
            totalRating: req.body.totalRating
        });
        return res.status(200).json({ name: newMovie, message: 'Movie added successfully!' });
    } catch (error) {
        return res.status(400).json({ status: 400, message: errorMessage.addNewMovie });
    }

This code works! I'm adding a new Movie and I get an OK status as expected.

Quick explaining about my code: I'm using MovieRepository where I use mongoose functionality to add a new movie. get all the info I need from the body and send it to the client.

Now, I've tried to change my code a little bit :

let newMovie = await MovieRepository.addNewMovie({
        name: req.body.name,
        year: req.body.year,
        description: req.body.description,
        length: req.body.length,
        genres: req.body.genres,
        totalRating: req.body.totalRating
    }).then(() => {
        return res.status(200).json({
            name: newMovie,
            message: 'Movie added successfully!'
        });
    }).catch((error) => {
        return res.status(400).json({
            status: 400,
            message: errorMessage.addNewMovie,
            systemError: error
        });
    });

This code is not working well.

Here I'm using the promise abilities to use then and catch. Pay attention that I didn't actually changed anything inside those functionalities.

In this case, the movie added to the database successfully as well, but the client gets status 400.

Any ideas what's happening here?

Upvotes: 1

Views: 82

Answers (1)

chillin
chillin

Reputation: 4486

I might be way off and incorrect in my understanding (new to JS) but I think newMovie is undefined inside your then call.

Try changing:

.then(() => {

to:

.then(newMovie => {

Also, I think your return statements in the two approaches (with promises and without promises) aren't doing the same thing -- which may have implications for the rest of your code.


This is my limited understanding of the differences between these two approaches:

  • First approach assigns the return value of addNewMovie to variable newMovie, and the outer async function returns whatever res.status(...).json(...) returns.
  • Second approach assigns the return value of the promise chain res.status(...).json(...) to newMovie -- and then the outer async function implicitly/automatically returns undefined (if there are no other return statements shown).
  • If you had moved your catch above the then, I think that would have allowed you to see that your addNewMovie was completing successfully (i.e. the movie was being added to the database), but that your success handler then was throwing an error.

I'm sure someone else can give you a better, more formal and accurate explanation.

Upvotes: 2

Related Questions