2

Hello I would like to get any help to get through this issue with my code.

so this is my server file with node js

//setup

var express = require("express");
var request = require("request");
var bodyParser = require("body-parser");
var app = express();

//make body-parser workin
app.use(bodyParser.urlencoded({extended: true}));
//help excluding ejs extension
app.set("view engine", "ejs");
//pointing express to look at public folders for css files. 
app.use(express.static("public"));


//routing starts
//root directory
app.get("/", function(req, res){
   res.render("home", { data : "" });
});

app.post("/results", function(req, res){
    var mname = req.body.movieName;
    var myear = req.body.movieYear;
    var url = "http://www.omdbapi.com/?s="+mname+"&y="+myear+"&apikey=$$$$$$$$$$$$";
    request(url, function(error, response, body){
    if(!error && response.statusCode == 200){
       var data = JSON.parse(body);
       res.render("home", { data : data });
       console.log(url);
    }else{
      console.log("ERRRORRRR WARNNNINNNNNNGG");
      console.log(error);
    }

    });

and this is part of my ejs file that want to access the JSON retrieved from API request

<%= data["Search"][0] %>

However, I get error like this

==================================================================== TypeError: /home/ubuntu/workspace/MovieSearchApp/views/home.ejs:32

32| <%= data["Search"][0] %>

Cannot read property '0' of undefined
at eval (eval at compile (/home/ubuntu/workspace/MovieSearchApp node_modules/ejs/lib/ejs.js:549:12), <anonymous>:11:40) 

etc

====================================================================

so I am assuming my way to access the JSON is somehow wrong so I looked at my JSON in detail json example

and I went back to my ejs file and tried

<%= data["Search"] %>

it gives me no error and shows up as

[object Object]

could somebody give me some direction to how to access this JSON correctly?

thanks for your time and help in advance

1 Answer 1

2

The most obvious issue is that you are passing data: "" in app.get("/",..

This means that when you go to your website's root / the data is a empty string.

Change it to

app.get("/", function(req, res){
   res.render("home");
});

and in the view check if data exists

<% if (data) { %>

<% } else { %>

<% } %>
Sign up to request clarification or add additional context in comments.

4 Comments

Hello Molda! when I removed the {data : "" } from get request, it gives me error as "data is not defined". So I tried to add that part again try to console.log(data) out (within the POST request after JSON.parse(body) line) and I can see JSON on my terminal windows. So it looks like the data is well retrieved from the API request however I am not sure how to properly access to it.
The thing is that you set data to "" in get request and then you're trying to access data['Search'][0] but data['Search'] is undefined. It is understandable that it works in app.post since you're retrieving the data from omdbapi.com. You are not doing that in get request though. You could change res.render("home"); to res.render("home", { data: { Search: [] } }); but even this won't work because data['Search'][0] will be undefined. You shouldn't try to access variables you are not sure exist.
Oh!!!!! I can see it now. I am going to try to separate the post request in different ejs file and let you know in minutes!!!!!!
Cool, glad i could help

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.