0

I am creating a rest api I have end point for Post/Movies: Request body should contain only movie title, and its presence should be validated Based on passed title, other movie details should be fetched from thememoviedb,and saved to application database.

here is what I have done so far :

app.post('/movies', (req, res) => {
    const movie = {
        title: req.body.title
    };
    request('https://api.themoviedb.org/3/discover/movie?callback=JSONP_CALLBACK&sort_by=popularity.desc&api_key=2931998c3a80d7806199320f76d65298', function (error, response, body) {
        console.log('error:', error); // Print the error if one occurred and handle it
        console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
        res.send(body)
      });
    db.collection('movies').insert(movie, (err, result) => {
        if (err) {
            res.send({
                'error': 'An error has occured'
            });
        } else {
            res.send(result.ops[0]);
        }
    }); });
};

This save only title and id, what do I need to do to achiev what I want ? newbie though. thanks

Hot Zellah
  • 1,061
  • 3
  • 11
  • 21
  • You want to `insert()` "inside" the callback response from the `request()`. Or discover what promises are and how to chain those together. Those two statement s in your current code do not wait for one or the other to complete. You also cannot `res.send()` more than once. Which is why the two things need to chain really. – Neil Lunn Jun 14 '18 at 10:14
  • am just learning can you provide some similar examples? @NeilLunn ? – Hot Zellah Jun 14 '18 at 10:27
  • Yup! [Looping Results with an External API Call and findOneAndUpdate](https://stackoverflow.com/a/50665957/2313887) – Neil Lunn Jun 14 '18 at 10:28
  • too complicated example for me , I will try finding something I can understand per my knowldge :( – Hot Zellah Jun 14 '18 at 10:33
  • I suggest you start with the two pretty hard to see links above your question as they essentially address the two main issues. Then you can take a look at something like that other link after you begin to understand. But it's not just an "example", but just happens to be exactly the same thing you are trying to do, but just in a loop of many instead of "one". So the same principles apply. Read the other posts first though if you can find them. The UI really needs to be better on making these duplicate question things much easier to spot when we put them there. – Neil Lunn Jun 14 '18 at 10:36
  • I will try my best , but if you can , can you just correct my code so that we can close this? thanks if you will, sorry for taking your time – Hot Zellah Jun 14 '18 at 10:44
  • I actually keep telling you to "look up". There will be no code corrections here, and this is not a code writing or corrections service. We do education though! And education starts with "scrolling up" instead of focusing on the comments. – Neil Lunn Jun 14 '18 at 10:47
  • Okay , I am doing using findoneandupdate thanks for the help :) – Hot Zellah Jun 14 '18 at 11:23

0 Answers0