'React native How to get only one part of my JSON back to Axios API
[beginner in React Native] I am working on the authentification. I have my call to API. It sends me back a JSON object with all the infos login which is inside a string called "j". How can I get only the the object inside the j and transform it into an object to use in my app? thank you for your help !
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
import base64 from "react-native-base64";
export const loginUser = createAsyncThunk(
'users/login',
async ({ codeClient, userEmail, password }, thunkAPI) => {
try
{
const response = await axios(
{
method: "post",
url: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
params:
{
AApplicationKey: "XXXXXXXXXXXXXXXXX",
AUriosCustomerID: "XXXXXXX",
AUserEmail: "XXXXXXXXXX",
AUserPwd: "XXXXXXXXX",
},
headers:
{
Authorization:
"Basic " +
base64.encode("xxxxxx" + ":" + "xxxxxxxxxxxxxxx"),
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
}
}
);
return JSON.parse(data.value[0].j);
} catch (e)
{
console.log('Error', e.response.data);
return thunkAPI.rejectWithValue(e.response.data);
}
}
);```
Solution 1:[1]
In your code, the "const response" isn't used.. you return "JSON.parse(data.value[0].j);" So it can't work.
Anyway your question is uncomplete without JSON answer is hard to say ^^
You should try
return JSON.parse(response.data.value[0].j);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | bastien |
