0

I'm attempting to save data from an API response and keep getting an empty object. I placed a console.log in my code and it shows that I'm getting a response from the api. I seem to be missing something after the fetch request.

From index.js:

const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const fetch = require('node-fetch')
const exphbs = require('express-handlebars')
const db = require('./src/models/movie')
require('./src/db/mongoose')
const Movie = require('./src/models/movie')

const app = express()
const port = process.env.PORT || 3000

// APP CONFIG
app.use(express.json())
app.use(bodyParser.urlencoded({extended: true}))

// ROUTES - ADD
app.post('/movies/:imdbID', (req, res) => {
    const imdb = req.params.imdbID
    const url = `**api link and key**${imdb}`

    const movie = fetch(url).then((res) => {
        return res.json()
    }).then((response) => {
        console.log(response)
        const addMovie = new Movie(response)
        db.Movie.create(addMovie, (err, newMovie) => {
            if(err){
                res.render('movie404')
            } else {
                res.redirect('/')
            }
        })
    }).catch((e) => {
        res.status(500).send(e)
    }) 
})

From mongoose.js:

const mongoose = require('mongoose')

mongoose.connect('mongodb://127.0.0.1:27017/movietime-api', {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true,
    useFindAndModify: false
})

From details.handlebars:

<h2>{{details.Title}}</h2>
<img src="{{details.Poster}}" alt="{{details.Title}}">
<p><em>{{details.Plot}}</em></p>
<p>Starring: {{details.Actors}}</p>
<p>Director: {{details.Director}}</p>
<form action="/movies/{{details.imdbID}}" method="POST">
    <button type="submit">Add</button>
</form>

From movie.js:

const mongoose = require("mongoose");

// MONGOOSE/MODEL CONFIG
const Movie = mongoose.model('Movie',{
    imdbID: String,
    Title: String,
    Poster: String,
    Director: String,
    Year: String,
    Plot: String,
    Ratings: String,
    Rated: String,
    Genre: String,
    Writer: String,
    Actors: String
});

module.exports = Movie;

I would expect a redirect to the index page then a new database entry using the above model.

1 Answer 1

1

you are almost there just few thing needs to be taken care of:


app.post('/movies/:imdbID', (req, res) => {
    const imdb = req.params.imdbID
    const url = `**api link and key**${imdb}`

    const movie = fetch(url).then((res) => {
        return res.json()
    }).then((response) => {
        console.log(response)
        const addMovie = new Movie(response)

        addMovie.save((err, newMovie) => {
            if(err){
                res.render('movie404',newMovie) // to render the details
            } else {
                res.redirect('/')
            }
        })
    }).catch((e) => {
        res.status(500).send(e)

ref

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.