I am trying to create a simple function which can be called in a React Native Screen, which returns an array with data.
I am importing the function here:
import React from 'react'
...
import { getvals } from '../components/Data_funks';
export default function ProjectsScreen({ route, navigation }) {
const { data } = route.params;
const mail = data.item.kontakte_mail;
// this is how I call the function:
getvals(data.item.projekt_bauleiter).then(response => console.log(response));
The array is displayed in the console but I cannot access the data of it. This is the function:
import React from 'react'
import { Image, StyleSheet } from 'react-native'
import clientConfig from '../../client-config';
export function getvals(user_id){
const siteUrl = clientConfig.siteUrl + '/wp-json/zep/v1/' + user_id;
return fetch(siteUrl,
{
method: "GET",
headers: {
Authorization: 'Bearer '+localStorage.getItem( 'token' )
},
})
.then((response) => response.json())
.then((responseData) => {
const error = responseData.data.error_code;
if(error == 0){
return responseData;
}else{
return 'error';
}
})
.catch(error => console.warn(error));
}
2
Answers
I dont know if this is a typo.. but it seems that you log the string ‘response’
You could save the
response
data in astate
.