Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: React js, Cannot read property "0". When calling an API

Thananjaya:

I cannot able to access the data from the Fetch Function. I want to pass the data from action to reducer. API is called using an fetch function, api is returned in the form of promise. So, API is called separately and data is returned back to the action payload.


import { INDEX_PRESCRIPTION } from '../constant.js';

function fetch_prescription(){
const base_url= "http://192.168.1.22:3000/api/v1/";
const fetch_url = `${base_url}/prescriptions`;
let datas = [];
fetch(fetch_url, {
method: "GET"
})
.then(response => response.json())
.then(data => {
datas.push(data['prescriptions'])
})
return datas
}

export const indexPrescription = () => {
const action = {
type: INDEX_PRESCRIPTION,
details: fetch_prescription()
}
console.log('details', action.details);
return action;
}

On examining the console, we get

console details is added as an image, click this link

How to access the prescription details. I tried to access it by action.details["0"]["0"] , but results in 'Cannot read property "0" of undefined '. I have gone through many questions and solution related to this problem, but cant able to study what is going wrong with my code.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: React js, Cannot read property "0". When calling an API

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×