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

                      Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

                      Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

                      How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page