[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);
}
}
);```
CodePudding user response:
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);
CodePudding user response:
Thank you I would like to verify with a code status 200 to check if it works well when the API sends me back the JSON object. Here is my code below :
export const loginUser = createAsyncThunk(
'users/login',
async ({ codeClient, userEmail, password }, thunkAPI) => {
try
{
const response = await axios(
{
method: "post",
url: "http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
params:
{
AApplicationKey: "fxxxxxxxxxxxxxxxxxxxxxxxxxb",
AUriosCustomerID: "XXXXXXXXXXX",
AUserEmail: "XXXXXXXXX",
AUserPwd: "XXXXXXXXXXXXXX",
},
headers:
{
Authorization:
"Basic "
base64.encode("XXXXXXXXXX" ":" "XXXXXXXXXXXXXXXXXX"),
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
}
}
);
// return JSON.parse(response.data.value[0].j);
if (response.status === 200)
{
console.log("ok");
console.log("Status", response.status);
return JSON.parse(response.data.value[0].j);
}
else
{
console.log("pas ok");
return thunkAPI.rejectWithValue('Erreur de connexion');
};
} catch (e)
{
console.log('Error', e.response.data);
return thunkAPI.rejectWithValue(e.response.data);
}
}
); ```
But I get this error message :
```Android Running app on SM-G960F
Is pending...
action payload undefined