The property length is undefined and cannot be read

I'm currently utilizing a dexi.io robot for the purpose of automating data extraction from permit databases. This particular robot has the capability to process custom JavaScript in order to dissect the incoming JSON object.

While this code does function correctly at times, successfully parsing the incoming JSON, it unfortunately fails to operate effectively in nearly all scenarios. An error message stating "cannot read property of length undefined" frequently appears.

If you'd like to test out the API that is functioning properly, click on the following link:

Below is the JavaScript code being used:

var people = JSON.parse(json).value;
var arr = [];

function getCountry(member) {
    try {
        return member.fields.name;
    } catch(err) {
        return "";
        }
}

for (i = 0; i < people.length; i++) {
    var member = people[i];
    var obj = 
        {
            "name": member.name,
            "Country": getCountry(member),
            "alias": member.alias
            };
    arr.push(obj);
}

return arr;

Answer №1

Make sure to review the initial line for the presence of "json". It seems like you may have overlooked defining a json file that is used to retrieve data.

Answer №2

If you're unsure about the process of fetching a response, I managed to make it work quite easily by following these steps:

fetch("https://services.odata.org/V4/TripPinService/People")
.then(response => response.json())
.then(data => {
  var people = data.value;
  var arr = [];
  function getCountry(member) {
      try {
          return member.AddressInfo[0].City.CountryRegion;
      } catch(err) {
          return "Country Info Not Available";
          }
  }
  
  for (i = 0; i < people.length; i++) {
      var member = people[i];
      var obj = 
          {
              "name": member.FirstName,
              "Country": getCountry(member),
              "alias": member.alias
              };
      arr.push(obj);
  }
  
  console.log(arr);
});

This method might inspire some innovation in your project.

Answer №3

After examining the json data from the provided URL, I noticed that in your code, properties such as 'member.name', 'member.alias', and 'member.fields.name' do not actually exist. I recommend using the precise property names as they appear in the json:

{
  "@odata.context": "http://services.odata.org/V4/TripPinService/$metadata#People",
  "@odata.nextLink": "https://services.odata.org/V4/TripPinService/People?%24skiptoken=8",
  "value": [
    {
      "@odata.id": "http://services.odata.org/V4/TripPinService/People('russellwhyte')",
      "@odata.etag": "W/\"08D817F8EC7DAC16\"",
      "@odata.editLink": "http://services.odata.org/V4/TripPinService/People('russellwhyte')",
      "UserName": "russellwhyte",
      "FirstName": "Russell",
      "LastName": "Whyte",
      "Emails": [
        "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83d1f6f0f0e6efefc3e6fbe2eef3efe6ade0ecee">[email protected]</a>",
        "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7725020404121b1b37141819031804185914181a">[email protected]</a>"
      ],
      "AddressInfo": [
        {
          "Address": "187 Suffolk Ln.",
          "City": {
            "CountryRegion": "United States",
            "Name": "Boise",
            "Region": "ID"
          }
        }
      ],
      "Gender": "Male",
      "Concurrency": 637285705159912400
    }
  ]
}

In essence, make sure to use the exact property names from the json data like 'member.UserName', 'member.AddressInfo[0].Name', etc.

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

Add .json files to your database

After successfully downloading tweets into a json file, the next step is to import it into a database using the following function: def import_json(fi): logging.warning("Loading tweets from json file {0}".format(fi)) for line in open(fi, "rb"): data ...

The button functionality gets hindered within the Bootstrap well

I'm trying to figure out what's wrong with my code. Here is the code: https://jsfiddle.net/8rhscamn/ <div class="well"> <div class="row text-center"> <div class="col-sm-1">& ...

showing sections that collapse next to each other

I am currently designing a portfolio website using HTML, CSS, and vanilla JavaScript. I have implemented collapsing sections that expand when clicked on. However, the buttons for these sections are stacked vertically and I want to place them side by side. ...

The value of a variable remains constant even when it is assigned within a function

I developed a dynamic image slideshow using HTML and JS specifically for the Lively Wallpaper app. This app offers various customization options, which are stored in the LivelyProperties.json file along with input types such as sliders and text boxes. To ...

Enter the UL element and modify the LI class if it contains the .has_children class

Seeking assistance in navigating through a UL element to modify the LI and nested UL classes when .has_children is detected. Take a look at this example: <ul class="nav navbar-nav"> <li class="first current parent">Link1</li> < ...

Discovering the data-id value in an HTML element by clicking it with JavaScript and returning that value through a for loop

this is my unique html content <ul class="dialogs"> {% if t_dialogs %} <li class="grouping">Today</li> {% for item in t_dialogs %} <li class=&qu ...

Developing a trivia game using HTML and JavaScript

I'm in need of some serious assistance with creating a quiz using HTML. My goal is to have a web page where users can visit, take a quiz, and have their responses stored. Unfortunately, I don't have the knowledge or skills required to do this on ...

Hero Sticky Transition with 100vhDivElement

My code is giving me some trouble. I want the div with text to stay at the bottom of the hero section with a transparent background. When that div reaches the top, it should stick with a black background. The issue is that I have set it in the javascript v ...

The resend email feature isn't functioning properly on the production environment with next js, however, it works seamlessly in the development environment

import { EmailTemplate } from "@/components/email-template"; import { Resend } from "resend"; const resend = new Resend("myApiKey"); // this works only in dev // const resend = new Resend(process.env.NEXT_PUBLIC_RESEND_API_KE ...

React Checkbox Sum Component - Effortlessly Improve User Experience

Looking for assistance with passing checkbox values to a React component in my app. I'm currently implementing a food list display using JSON data, but I'm unsure how to transfer the checkbox values to another component. For reference, here&apos ...

AJAX requests sent from different origins to AWS S3 may encounter CORS errors on occasion

My current objective is to access publicly available files stored in S3. The CORS configuration for my S3 setup is as follows: <?xml version="1.0" encoding="UTF-8"?> <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> < ...

Place the object into a vacant area with the help of jQuery Sortable

I am using jQuery Sortable and it's functioning well. However, I have encountered a small issue that I need help with.    I have two blocks where elements from one block can be moved to the other. The problem arises when all the elem ...

Updating the image source through ajax by retrieving the location from the database

Is there a way to dynamically change the image source using AJAX? I have the location saved in my database and I want to set the img src from the value of something like data[0]['patient_photo']. Below is the HTML code for the image: <img id= ...

Setting the second tab as the primary active tab

I am currently working on a script that is well-known, and everything is functioning perfectly. However, I want to change it so that when the page is first opened, it displays the second tab instead of the first one (the first tab being a mail compose tab ...

React Datepicker on Safari: A seamless way to pick dates

While working on my application, I encountered an issue with the Form.Input functionality from Semantic UI React library. I am using it to insert dates and found that it displays a date-picker on Chrome and Firefox but not on Safari. I attempted to use the ...

The rule "react/jsx-sort-props" does not have a valid configuration

I've been attempting to organize props names alphabetically using the eslint-plugin-react plugin but I keep encountering this error: [Error ] .eslintrc.json: Configuration for rule "react/jsx-sort-props" is invalid: Value {"callbacksLast":true,"shorth ...

Unable to include JSONParser in Android Studio project

Recently, I've been working on a Project that involves JSON and Java. After following the instructions here, I added the Java file as a dependency in my build.gradle. Even though I imported the Json package, I encountered an issue where I couldn' ...

Bootstrap 4 Nav Table of Contents with Expand and Collapse Feature

Currently, I am encountering an issue with implementing a button to expand and collapse a "table of contents" in Bootstrap 4. The code I have so far can be viewed here: https://codepen.io/nht910/pen/RwwwyKB Code Snippet: <div class="main-wrapper col- ...

Codeigniter session unexpectedly ends after AJAX call completes successfully

Currently, I am utilizing the CodeIgniter framework on a Heroku server with an AWS database. In my AJAX success function, I have a window.location.reload(); call which ends up destroying the session and redirecting to the login page. Is there a way to prev ...

Locate relevant information within the arrays through the process of filtering

For my collection, I am looking to match the operator_name based on date and then further narrow it down by matching shift_name within an array { "_id": "5eb301bc0218ff48b724a486", "date": "2020-07-02T00:00:00.000Z", "shift_wise": [{ "_id": ...