0

I am trying to pull JSON data from Shipstation. All i want to do at this point is store the data in a state object called Orders and render the shipment IDs to a page. I am getting the following error:

Line 25: 'orders' is not defined no-undef

import React, { Component } from "react";
class Orders extends Component {
  constructor(props) {
    super(props);
    this.state = {
      orders: []
    };
  }

  componentDidMount() {
    let key = "[MY KEY GOES HERE]";
    let secret = "[MY SECRET GOES HERE]";
const encodedString = new Buffer(key + ":" + secret).toString("base64");

fetch("https://ssapi.shipstation.com/orders/", {
  headers: {
    Authorization: "Basic " + encodedString
  }
})
  .then(res => res.json())
  .then(data => {
    this.setState({ orders: data });
    console.log(orders);
      })
      .catch(err => console.log(err));
  }

  render() {
    const { orders } = this.state;

const orderItems = orders.map(order => (
  <div key={order.id} className="card card-body mb-2">
    <div className="row">
      <div className="col-md-6">
        <p>{order.orderId}</p>
      </div>
    </div>
  </div>
));
return (
  <div ref="myRef">
    <hr />
    <h3 className="mb-4">Recent Shipments</h3>
    {orderItems}
  </div>
);
  }
}

export default Orders;

1 Answer 1

1

That looks like an ESLint error, and it's just saying that you can't do

console.log(orders);

because you haven't actually defined the variable anywhere at that point.

You could change your code to:

const orders = data;
this.setState({ orders });
console.log(orders)

and everything should work fine.

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

5 Comments

The constructor props has a state array defined as 'orders'. From the componentDidMount() function I'm trying to store the JSON data in that state using setState() and console log the content of the orders state. Am I logging the wrong output? If I remove the console log statement, THAT error goes away and I receive an error saying: TypeError: orders.map is not a function
Ah, you're right – sorry. If you're trying to log state then you'll need to do: console.log(this.state.orders).
This consoles the data and there are no errors caught from calling fetch(). However, I am still receiving a TypeError: orders.map is not a function. The orders state is assigned as an array. This error should only display if the orders state was assigned as something else (ie. string or object)
The data coming back from your API probably isn't an array like you're expecting.
This was fixed by setting orders.map() to this.state.orders.map()

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.