0

I've checked other responses and couldn't figure this out. I can't tell if the data I'm getting back is just in correct or if my loop is not right. I'm trying to access the values of this array in my response.

Using a node library I'm accessing Zillow api with this code:

const Zillow = require("node-zillow")

const zillow = new Zillow('my key')

const parameters = {
    address: "5555 Ronald Road",
    citystatezip: "South Gate, CA",
    rentzestimate: true
}

zillow.get('GetSearchResults', parameters)
    .then(results => {
        console.log(results)
        return results
    })

This returns the following:

{ request: { address: '5555 Ronald Road', citystatezip: 'South Gate, CA' },
  message: { text: 'Request successfully processed', code: '0' },
  response: { results: { result: [Array] } } }

My problem is I'm unable to access the data in the Array. I've never used Node before so I'm confused as how to do it.

I try adding this:

for (let item of results) {
  console.log(results)
}

Since it's an array I figured I could do this console.log(results[0]) this returns undefined.

FabricioG
  • 3,107
  • 6
  • 35
  • 74

1 Answers1

0

If what you're showing in your second code block is what you saw when you did console.log(results) inside the .then() handler, then the array would be in results.response.results.result. That's how you navigate into the sub-objects in the object that your zillow.get() call resolves to.

You can do console.log(results.response.results.result) to see the contents of the array in side the .then() handler.

zillow.get('GetSearchResults', parameters).then(results => {
    let resultsArray = results.response.results.result;
    console.log(resultsArray);
    for (let item of resultsArray) {
        console.log(item);
    }
    return resultsArray;
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979