React Native : Fetch 在通过引用传递时无法检索令牌的值



我正试图从下面的代码中获得响应。预期的响应是400。当我在fetch调用中显式键入ut时,它就起作用了。但当我作为变量传递出去时,我会得到低于的错误

注意:retrieveUserToken((从AsyncStorage获取令牌。

WHat有效:当我在标题中键入它时:它有效。

'Authorization': "Bearer eyJskfjvksndvjnsdknvlkandlkcnals"
Doesn't work: 
```'Authorization': "Bearer "+ ut
```onPress={async () => {

const ut = await retrieveUserToken("token")
const data = {id: "myID", password:"mypass"}
await fetch("http://MY-URL", {
method: "POST",

headers: {
"Content-Type": "application/json",
'Authorization':
"Bearer " + ut
},
body: JSON.stringify(data),
}).then(response => JSON.stringify( response))
.then(result => console.log(result))
.catch(error => console.log('error', error));
}}

ERROR:  {"type":"default","status":401,"ok":false,"headers":{"map":{"content-length":"0","server":"Kestrel","www-authenticate":"Bearer error="invalid_token"","date":"Fri, 21 Aug 2020 03:23:00 GMT","content-type":"text/plain"}},"url":"http://63.35.183.253:9054/api/appointment","bodyUsed":false,"_bodyInit":{"_data":{"size":0,"offset":0,"blobId":"F5F055F9-8C4C-434D-AA39-11B58B0CE290","type":"text/plain","name":"appointment.txt","__collector":{}}},"_bodyBlob":{"_data":{"size":0,"offset":0,"blobId":"F5F055F9-8C4C-434D-AA39-11B58B0CE290","type":"text/plain","name":"appointment.txt","__collector":{}}}}

"www-authenticate":"Bearer error="invalid_token"

这意味着ut === 'error="invalid_token"'。也就是说,您在检索令牌时遇到问题。当你回头看的时候:

const ut = await retrieveUserToken("token")

您确定要将字符串"token"作为arg传递给吗?

最新更新