Having trouble retrieving JavaScript object properties?

I am facing an issue with a Javascript object that has been parsed from a JSON result. Despite having all the properties defined, I am getting 'undefined' for each of them.

Here is the code snippet:

var yelpObj = JSON.parse(result);
console.log(yelpObj);
document.getElementById('yelp-rating').src=yelpObj.rating_img_url;

Upon checking my log:

{
  "is_claimed": false,
  "rating": 3.5,
  "mobile_url": "http://m.yelp.com/biz/o-griffs-grill-and-brewhouse-quincy",
  "rating_img_url": "http://s3-media1.fl.yelpcdn.com/assets/2/www/img/5ef3eb3cb162/ico/stars/v1/stars_3_half.png",
  "review_count": 12,
  "name": "O'Griff's Grill and Brewhouse",
  ...

Despite this, the src attribute remains "undefined". Can anyone help me identify what the issue might be? I have also attempted accessing the property using array notation without success.

Answer №1

Experiment

Modify the element with ID 'yelp-rating' to display the Yelp rating image using the URL from yelpObj.rating_img_url.

Answer №2

My apologies for the earlier mistake - upon reevaluation, it seems that the log showed that it was not a Javascript object after all. The data retrieved from Yelp is already in JSON format, so there was no need to encode it on the PHP side. The revised log now displays as follows:

Object {is_claimed: false, rating: 3.5, mobile_url: "http://m.yelp.com/biz/o-griffs-grill-and-brewhouse-quincy", rating_img_url: "http://s3-media1.fl.yelpcdn.com/assets/2/www/img/5ef3eb3cb162/ico/stars/v1/stars_3_half.png", review_count: 12…}

This demonstrates that the data is indeed an object, as originally intended.

I can confirm that the accessors are functioning correctly. Once again, I apologize for any confusion caused and appreciate you reviewing this matter.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Continue populating content within an iframe even after it is redirected to various URLs

I am familiar with how to write content into an iframe: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Content Writer</title> <style> textarea, iframe { ...

Getting the Height and Width of an Image during the upload process in a React application

Can anyone help me with retrieving the Image Height and Width during image upload? I have attempted the following code, but it always gives me 0 0: const uploadedImage = e.target.files[0]; var image = new Image(); image.src = uploadedImage; console.log( ...

Steps to access the parent node key in Realtime Firebase Database

When data is pushed into the RealTime Firebase Database, it gets structured in JSON format. However, if there is a JSONObject within another JSONObject in the RealTime Database, how can we retrieve the "key" name of that inner JSONObject? The image below ...

Guide for eliminating a specific row from a list in AngularJS

Despite my thorough search, I couldn't find a solution to my problem of deleting the CORRECT row from the list. Let's say I have the following array: $scope.rows = [{ "ID": 12, "customer": "abc", "image": "abc.jpg", },{ ...

Is it possible to employ JavaScript for performing a Ctrl-F5 refresh?

I'm working with a small asp file that runs in a frame. Is there a way to trigger a CTRL+F5 type of refresh to reload the entire browser window? I've attempted using parent.location.reload(true), location.reload(), and various other methods, but ...

Sending Laravel validation errors back to an Ajax request

Need help with passing validation errors back to my ajax call in order to stop the page from progressing and show those errors to the user. /* * Creating a new user after completing the initial part of the form. * */ public function createUserAndOrder(R ...

Having trouble getting automapper-ts registered in the Global Context within an Angular application - Reference to App is Undefined

Ever since the upgrade to Angular 18, it seems like the node package automapper-ts has stopped functioning properly. When attempting to set up Automapper in the global context, the issue arises where app is undefined, resulting in the failure to resolve t ...

Is CDATA insertion automatic in JavaScript within Yii framework?

Currently I am working with Yii and have noticed that whenever I insert any JavaScript code, it is automatically encapsulated in CDATA. I am curious as to why this is happening. Will there be any issues if I were to remove the CDATA tags, considering that ...

An alternative to PHP's exec function in Node.js

I am interested in developing a piece of software using C (such as prime number factorization) and hosting it on my web server with Node.js. Following that, I would like to create an HTML document containing a form and a button. Upon clicking the button, ...

Just starting out with jQuery: seeking advice on a user-friendly slideshow plugin, any tips on troubleshooting?

I am currently trying to incorporate a basic jquery slideshow plugin, but I seem to be encountering some difficulties. The documentation mentions the need to install 'grunt' and 'node dependencies', which is confusing to me as I am new ...

Enhancing Page Content Dynamism: Making Elements Static

I have a div element that I want to align on the right side of the screen, but the problem is that it doesn't adjust its position as the content dynamically expands and exceeds the width of the page. Despite conducting extensive research, I haven&apos ...

Ways to detect the scroll event on a scrollable container?

https://i.sstatic.net/Vx3M7.jpg I am looking to implement a scroll event listener on a specific element, where an arrow icon will appear when the user scrolls up in the message box. Similar to the functionality seen in the Youtube live chat image above. ...

The React application is showing an empty page without any visible errors during the compilation process

I'm having trouble with my React app - it compiles without errors but only shows a blank page. Can someone help me figure out what's wrong with my code? I'm new to React and could use some guidance. index.js import React from 'react&ap ...

Tips for resolving a double click issue with a jQuery slide up/down animation

When I click a button, there is an animation that makes a div slide up and then down again. It functions the way I want it to, but the first time you click it, it doesn't work; you have to click twice for it to respond. Someone recommended using... ...

Tips for splitting a container of specific height into sections measuring 80% and 20%

I am working on a container with a fixed position that I want to split into two halves: 80% and 20% at the bottom. This is what I want it to look like: Click here to see the image. Note: The layout should adjust itself when the window is resized. You c ...

Sending an array from PHP to Javascript using PHP can be done by creating the array

Looking for assistance on passing an array of values from PHP and retrieving it using AJAX. I've only been able to pass a single value so far, and I'm unsure how to handle arrays on the AJAX side. Here's my PHP code: $success[]; $timeout[] ...

Guidelines on extracting JSON data from an API

After developing an API, the output from http://localhost/lsapp4/public/api/articles in Postman looks like this: { "data": [ { "id": 1, "title": "Demo Article-1", "body": "H ...

What is the process for adjusting the code to manage the labels of specific buttons?

There is a code $("button").click(function() { var btn = this; if (btn.loaded) { $(btn).prev("div").html(btn.originalContent); btn.loaded = false; $(btn).text('Get Dynamic chart'); } else { btn.originalConte ...

Update and modify content.php directly on the samepage.php rather than through externalpage.php by utilizing either Jquery or AJAX

Is there a preferred method for loading content on the same page instead of redirecting to an external page using AJAX or JQuery? Below is an excerpt from my external.php file: $id = null; if (!empty($_GET['id'])) { $id = $_REQUEST[ ...

Verify whether the chosen options from a dropdown list coincide with a predetermined value

I am working with a form that generates dropdown lists dynamically. <c:forEach var="companyList" items="${company.detailList}" varStatus="status"> <tr> <td>c:out value="${companyList.employeeName}" /></td> <td> <select ...