不能使用异步函数的值
Value of async function can't be used
我将令牌保存到 asyncstorage 中以供检索以进行身份验证,但问题是每当我获得该值时,我都会将其登录到控制台并且它可以工作但它不会将令牌发送到 header,我尝试了几种方法,但所有逻辑似乎都流产了。请帮助您体验,还是我在使用 运行 异步存储?
export const editProfile = (userData) => {
const storageGet = async () => {
try {
const result = await AsyncStorage.getItem("token");
console.log("data", result);
console.warn("data", result);
return result;
} catch(error) {
console.log(error);
}
}
return (dispatch, getState) => {
const userId = getState().auth._id
axios.put(`${url}/users/${userId}`, userData,
(async () => { // block of code to set auth token to header
try{
const x = await storageGet()
console.log("nowToken", x) // it returns the token into the console
setHeaders(x) // but didn't set the token in the header
}catch(err) {
console.log(err.response)
}
})()
// setHeaders(token)
)
.then(response => {
dispatch({
type: "EDIT_PROFILE_SUCCESS",
username: response.data.username,
email: response.data.email,
name: response.data.name,
phoneNumber: response.data.phoneNumber,
biography: response.data.biography,
gender: response.data.gender,
address: response.data.address,
message: response.data.message,
success: response.data.success
})
})
.catch((error) => {
console.log(error.response)
})
}
}
根据 axios 文档,api 是 axios.put(url[, data[, config]])
您的第三个参数(代表配置)是一个不返回任何内容的函数。将其替换为 returns 您想要的配置对象的函数。例如()=>{'authorization': await AsyncStorage.getItem("token")}
这应该可以解决问题。
这可能更具可读性。
const token = await AsyncStorage.getItem("token")
const config = {headers: {'authorization':`bearer ${token}`}}
axios.put(`${url}/users/${userId}`, userData, config)
编辑 片段丢失 'headers' 键。
我将令牌保存到 asyncstorage 中以供检索以进行身份验证,但问题是每当我获得该值时,我都会将其登录到控制台并且它可以工作但它不会将令牌发送到 header,我尝试了几种方法,但所有逻辑似乎都流产了。请帮助您体验,还是我在使用 运行 异步存储?
export const editProfile = (userData) => {
const storageGet = async () => {
try {
const result = await AsyncStorage.getItem("token");
console.log("data", result);
console.warn("data", result);
return result;
} catch(error) {
console.log(error);
}
}
return (dispatch, getState) => {
const userId = getState().auth._id
axios.put(`${url}/users/${userId}`, userData,
(async () => { // block of code to set auth token to header
try{
const x = await storageGet()
console.log("nowToken", x) // it returns the token into the console
setHeaders(x) // but didn't set the token in the header
}catch(err) {
console.log(err.response)
}
})()
// setHeaders(token)
)
.then(response => {
dispatch({
type: "EDIT_PROFILE_SUCCESS",
username: response.data.username,
email: response.data.email,
name: response.data.name,
phoneNumber: response.data.phoneNumber,
biography: response.data.biography,
gender: response.data.gender,
address: response.data.address,
message: response.data.message,
success: response.data.success
})
})
.catch((error) => {
console.log(error.response)
})
}
}
根据 axios 文档,api 是 axios.put(url[, data[, config]])
您的第三个参数(代表配置)是一个不返回任何内容的函数。将其替换为 returns 您想要的配置对象的函数。例如()=>{'authorization': await AsyncStorage.getItem("token")}
这应该可以解决问题。
这可能更具可读性。
const token = await AsyncStorage.getItem("token")
const config = {headers: {'authorization':`bearer ${token}`}}
axios.put(`${url}/users/${userId}`, userData, config)
编辑 片段丢失 'headers' 键。