Can't get token - using async storage method - react native(Expo) The Next CEO of Stack OverflowHide keyboard in react-nativeWhat is the difference between using constructor vs getInitialState in React / React Native?React Native android build failed. SDK location not foundWhat is the difference between React Native and React?What is the difference between Expo and React Native?Not using the Expo fork of react-nativeReact Native Expo Camerareact-native expo issue: check method of `SceneView`undefined-is-not-an-object-evaluating-this-state-datasource.mapExpo, React Native Async Storage resets after hot reload?

How do I go from 300 unfinished/half written blog posts, to published posts?

Grabbing quick drinks

What can we do to stop prior company from asking us questions?

Is a stroke of luck acceptable after a series of unfavorable events?

MAZDA 3 2006 (UK) - poor acceleration then takes off at 3250 revs

What happens if you roll doubles 3 times then land on "Go to jail?"

Shade part of a Venn diagram

Why did we only see the N-1 starfighters in one film?

Why didn't Theresa May consult with Parliament before negotiating a deal with the EU?

India just shot down a satellite from the ground. At what altitude range is the resulting debris field?

Does the Brexit deal have to be agreed by both Houses?

When airplanes disconnect from a tanker during air to air refueling, why do they bank so sharply to the right?

Anatomically Correct Mesopelagic Aves

Is it safe to use c_str() on a temporary string?

Need some help with wall behind rangetop

Failed to fetch jessie backports repository

When Does an Atlas Uniquely Define a Manifold?

How long to clear the 'suck zone' of a turbofan after start is initiated?

Implement the Thanos sorting algorithm

Why does standard notation not preserve intervals (visually)

Trouble understanding the speech of overseas colleagues

How to get regions to plot as graphics

% symbol leads to superlong (forever?) compilations

Should I tutor a student who I know has cheated on their homework?



Can't get token - using async storage method - react native(Expo)



The Next CEO of Stack OverflowHide keyboard in react-nativeWhat is the difference between using constructor vs getInitialState in React / React Native?React Native android build failed. SDK location not foundWhat is the difference between React Native and React?What is the difference between Expo and React Native?Not using the Expo fork of react-nativeReact Native Expo Camerareact-native expo issue: check method of `SceneView`undefined-is-not-an-object-evaluating-this-state-datasource.mapExpo, React Native Async Storage resets after hot reload?










0















I'm working on a feedback form and I'm having trouble getting token from the server after I logged into a random user account. This is my code:



getToken function:



getToken = async () => 
try
const value = await AsyncStorage.getItem("token");
this.setState( userToken: value );
catch (error)
console.log("Error retrieving data" + error);

;


submit function:



postFeedback(userToken, title, content, to_id, category) 
fetch(
"https://deployattendancemanagement.herokuapp.com/api/feedback/send",

method: "POST",
headers:
"Content-Type": "application/x-www-form-urlencoded"
,
body: JSON.stringify(
token: userToken,
title: title,
content: content,
to_id: to_id,
category: category
)

)
.then(response => response.json())
.then(res =>
if (typeof res.result != "success")
console.log(res.message);
Alert.alert(
"Oops !",
"Something went wrong",
[

text: "OK",
onPress: () => console.log("Cancel Pressed"),
style: "cancel"

],
cancelable: false
);
else
console.log("success");
Alert.alert(
"Yay !",
"Something went right",
[

text: "OK",
onPress: () => console.log("Send feedback"),
style: "cancel"

],
cancelable: false
);

)
.catch(error =>
console.error(error);
);



Button:



<Button
full
onPress=() =>
this.postFeedback(
this.state.userToken,
this.state.title,
this.state.content,
this.state.to_id,
this.state.category
)

>
<Text>SUBMIT</Text>
</Button>


But after I pressed the button, the title and content were not sent and it gives me this error: No Token Provided. Please help, I'm very new at react-native programming.










share|improve this question
























  • I also see you are using both promises and async awaits syntax. I suggest to stick to one of them as that approach might just clear up some things

    – stephanmantel
    Mar 8 at 13:51















0















I'm working on a feedback form and I'm having trouble getting token from the server after I logged into a random user account. This is my code:



getToken function:



getToken = async () => 
try
const value = await AsyncStorage.getItem("token");
this.setState( userToken: value );
catch (error)
console.log("Error retrieving data" + error);

;


submit function:



postFeedback(userToken, title, content, to_id, category) 
fetch(
"https://deployattendancemanagement.herokuapp.com/api/feedback/send",

method: "POST",
headers:
"Content-Type": "application/x-www-form-urlencoded"
,
body: JSON.stringify(
token: userToken,
title: title,
content: content,
to_id: to_id,
category: category
)

)
.then(response => response.json())
.then(res =>
if (typeof res.result != "success")
console.log(res.message);
Alert.alert(
"Oops !",
"Something went wrong",
[

text: "OK",
onPress: () => console.log("Cancel Pressed"),
style: "cancel"

],
cancelable: false
);
else
console.log("success");
Alert.alert(
"Yay !",
"Something went right",
[

text: "OK",
onPress: () => console.log("Send feedback"),
style: "cancel"

],
cancelable: false
);

)
.catch(error =>
console.error(error);
);



Button:



<Button
full
onPress=() =>
this.postFeedback(
this.state.userToken,
this.state.title,
this.state.content,
this.state.to_id,
this.state.category
)

>
<Text>SUBMIT</Text>
</Button>


But after I pressed the button, the title and content were not sent and it gives me this error: No Token Provided. Please help, I'm very new at react-native programming.










share|improve this question
























  • I also see you are using both promises and async awaits syntax. I suggest to stick to one of them as that approach might just clear up some things

    – stephanmantel
    Mar 8 at 13:51













0












0








0








I'm working on a feedback form and I'm having trouble getting token from the server after I logged into a random user account. This is my code:



getToken function:



getToken = async () => 
try
const value = await AsyncStorage.getItem("token");
this.setState( userToken: value );
catch (error)
console.log("Error retrieving data" + error);

;


submit function:



postFeedback(userToken, title, content, to_id, category) 
fetch(
"https://deployattendancemanagement.herokuapp.com/api/feedback/send",

method: "POST",
headers:
"Content-Type": "application/x-www-form-urlencoded"
,
body: JSON.stringify(
token: userToken,
title: title,
content: content,
to_id: to_id,
category: category
)

)
.then(response => response.json())
.then(res =>
if (typeof res.result != "success")
console.log(res.message);
Alert.alert(
"Oops !",
"Something went wrong",
[

text: "OK",
onPress: () => console.log("Cancel Pressed"),
style: "cancel"

],
cancelable: false
);
else
console.log("success");
Alert.alert(
"Yay !",
"Something went right",
[

text: "OK",
onPress: () => console.log("Send feedback"),
style: "cancel"

],
cancelable: false
);

)
.catch(error =>
console.error(error);
);



Button:



<Button
full
onPress=() =>
this.postFeedback(
this.state.userToken,
this.state.title,
this.state.content,
this.state.to_id,
this.state.category
)

>
<Text>SUBMIT</Text>
</Button>


But after I pressed the button, the title and content were not sent and it gives me this error: No Token Provided. Please help, I'm very new at react-native programming.










share|improve this question
















I'm working on a feedback form and I'm having trouble getting token from the server after I logged into a random user account. This is my code:



getToken function:



getToken = async () => 
try
const value = await AsyncStorage.getItem("token");
this.setState( userToken: value );
catch (error)
console.log("Error retrieving data" + error);

;


submit function:



postFeedback(userToken, title, content, to_id, category) 
fetch(
"https://deployattendancemanagement.herokuapp.com/api/feedback/send",

method: "POST",
headers:
"Content-Type": "application/x-www-form-urlencoded"
,
body: JSON.stringify(
token: userToken,
title: title,
content: content,
to_id: to_id,
category: category
)

)
.then(response => response.json())
.then(res =>
if (typeof res.result != "success")
console.log(res.message);
Alert.alert(
"Oops !",
"Something went wrong",
[

text: "OK",
onPress: () => console.log("Cancel Pressed"),
style: "cancel"

],
cancelable: false
);
else
console.log("success");
Alert.alert(
"Yay !",
"Something went right",
[

text: "OK",
onPress: () => console.log("Send feedback"),
style: "cancel"

],
cancelable: false
);

)
.catch(error =>
console.error(error);
);



Button:



<Button
full
onPress=() =>
this.postFeedback(
this.state.userToken,
this.state.title,
this.state.content,
this.state.to_id,
this.state.category
)

>
<Text>SUBMIT</Text>
</Button>


But after I pressed the button, the title and content were not sent and it gives me this error: No Token Provided. Please help, I'm very new at react-native programming.







react-native expo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 13:42









Mahdi NBA

187




187










asked Mar 7 at 16:30









thinh-phanthinh-phan

13




13












  • I also see you are using both promises and async awaits syntax. I suggest to stick to one of them as that approach might just clear up some things

    – stephanmantel
    Mar 8 at 13:51

















  • I also see you are using both promises and async awaits syntax. I suggest to stick to one of them as that approach might just clear up some things

    – stephanmantel
    Mar 8 at 13:51
















I also see you are using both promises and async awaits syntax. I suggest to stick to one of them as that approach might just clear up some things

– stephanmantel
Mar 8 at 13:51





I also see you are using both promises and async awaits syntax. I suggest to stick to one of them as that approach might just clear up some things

– stephanmantel
Mar 8 at 13:51












2 Answers
2






active

oldest

votes


















0














You have not called getToken() function in your code. That would be the problem here.






share|improve this answer
































    0














    From what I understand, you are not saving the token. You need to save it before you can retrieve it. It would be helpful if you could share the code where you receive the token but here's the general syntax of saving something to AsyncStorage. Also make sure you call the getToken() function.



    saveToken = async () => 
    let token = 'something';
    await AsyncStorage.setItem('key', token);
    ;





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55048615%2fcant-get-token-using-async-storage-method-react-nativeexpo%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You have not called getToken() function in your code. That would be the problem here.






      share|improve this answer





























        0














        You have not called getToken() function in your code. That would be the problem here.






        share|improve this answer



























          0












          0








          0







          You have not called getToken() function in your code. That would be the problem here.






          share|improve this answer















          You have not called getToken() function in your code. That would be the problem here.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 9 at 17:52









          Bhargav Rao

          30.9k2092114




          30.9k2092114










          answered Mar 8 at 13:11









          Mahdi NBAMahdi NBA

          187




          187























              0














              From what I understand, you are not saving the token. You need to save it before you can retrieve it. It would be helpful if you could share the code where you receive the token but here's the general syntax of saving something to AsyncStorage. Also make sure you call the getToken() function.



              saveToken = async () => 
              let token = 'something';
              await AsyncStorage.setItem('key', token);
              ;





              share|improve this answer



























                0














                From what I understand, you are not saving the token. You need to save it before you can retrieve it. It would be helpful if you could share the code where you receive the token but here's the general syntax of saving something to AsyncStorage. Also make sure you call the getToken() function.



                saveToken = async () => 
                let token = 'something';
                await AsyncStorage.setItem('key', token);
                ;





                share|improve this answer

























                  0












                  0








                  0







                  From what I understand, you are not saving the token. You need to save it before you can retrieve it. It would be helpful if you could share the code where you receive the token but here's the general syntax of saving something to AsyncStorage. Also make sure you call the getToken() function.



                  saveToken = async () => 
                  let token = 'something';
                  await AsyncStorage.setItem('key', token);
                  ;





                  share|improve this answer













                  From what I understand, you are not saving the token. You need to save it before you can retrieve it. It would be helpful if you could share the code where you receive the token but here's the general syntax of saving something to AsyncStorage. Also make sure you call the getToken() function.



                  saveToken = async () => 
                  let token = 'something';
                  await AsyncStorage.setItem('key', token);
                  ;






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 9 at 19:08









                  fsocietyfsociety

                  9619




                  9619



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55048615%2fcant-get-token-using-async-storage-method-react-nativeexpo%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                      Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                      2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived