Cannot fetch data from mongo db react native





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am brand new to React Native and have just figured out setting up a local server and db for my native app. I am using express and mongodb. I have my server and mongo running on my backend and am trying to make an axios call to get the data from my database but the array is coming back empty and the response from the data is undefined. I have tested the endpoints on postman and the endpoints are fine. Here is an example of my api axios request and the component I am trying to get the data back in.
Thank you in advanced for your help! Also if you would like to see any other code let me know and I will post it.



// api.js

import axios from "axios";

export const getAllTweets = async () => {
await axios
.get("http://(my ip address):(port number)/api/tweet")
.then(response => {
console.log("response: ", response);
return response.data;
})
.catch(error => {
console.log("There are no tweets to get!", error);
});
};





//HomeScreen.js

import React from 'react';
import {
Image,
Platform,
ScrollView,
StyleSheet,
Text,
TouchableOpacity,
View,
} from 'react-native';
import { WebBrowser } from 'expo';

import { getAllTweets } from '../constants/api';

import { MonoText } from '../components/StyledText';

export default class HomeScreen extends React.Component {
static navigationOptions = {
header: null,
};
static defaultProps = {
getAllTweets
};
state = {
tweets: ,
isLoading: false
}

async componentDidMount() {
this.setState({ isLoading: true });
let data = await this.props.getAllTweets();
console.log('data: ', data);
try {
this.setState({ isLoading: false, tweets: data });
} catch (error) {
console.log(error, "Cannot get tweets in cdm");
}
}

render() {
console.log("this.state.tweets", this.state.tweets); <--- this is empty
return (
<View style={styles.container}>

</View>
);
}

_maybeRenderDevelopmentModeWarning() {
if (__DEV__) {
const learnMoreButton = (
<Text onPress={this._handleLearnMorePress} style={styles.helpLinkText}>
Learn more
</Text>
);

return (
<Text style={styles.developmentModeText}>
Development mode is enabled, your app will be slower but you can use useful development
tools. {learnMoreButton}
</Text>
);
} else {
return (
<Text style={styles.developmentModeText}>
You are not in development mode, your app will run at full speed.
</Text>
);
}
}









share|improve this question





























    0















    I am brand new to React Native and have just figured out setting up a local server and db for my native app. I am using express and mongodb. I have my server and mongo running on my backend and am trying to make an axios call to get the data from my database but the array is coming back empty and the response from the data is undefined. I have tested the endpoints on postman and the endpoints are fine. Here is an example of my api axios request and the component I am trying to get the data back in.
    Thank you in advanced for your help! Also if you would like to see any other code let me know and I will post it.



    // api.js

    import axios from "axios";

    export const getAllTweets = async () => {
    await axios
    .get("http://(my ip address):(port number)/api/tweet")
    .then(response => {
    console.log("response: ", response);
    return response.data;
    })
    .catch(error => {
    console.log("There are no tweets to get!", error);
    });
    };





    //HomeScreen.js

    import React from 'react';
    import {
    Image,
    Platform,
    ScrollView,
    StyleSheet,
    Text,
    TouchableOpacity,
    View,
    } from 'react-native';
    import { WebBrowser } from 'expo';

    import { getAllTweets } from '../constants/api';

    import { MonoText } from '../components/StyledText';

    export default class HomeScreen extends React.Component {
    static navigationOptions = {
    header: null,
    };
    static defaultProps = {
    getAllTweets
    };
    state = {
    tweets: ,
    isLoading: false
    }

    async componentDidMount() {
    this.setState({ isLoading: true });
    let data = await this.props.getAllTweets();
    console.log('data: ', data);
    try {
    this.setState({ isLoading: false, tweets: data });
    } catch (error) {
    console.log(error, "Cannot get tweets in cdm");
    }
    }

    render() {
    console.log("this.state.tweets", this.state.tweets); <--- this is empty
    return (
    <View style={styles.container}>

    </View>
    );
    }

    _maybeRenderDevelopmentModeWarning() {
    if (__DEV__) {
    const learnMoreButton = (
    <Text onPress={this._handleLearnMorePress} style={styles.helpLinkText}>
    Learn more
    </Text>
    );

    return (
    <Text style={styles.developmentModeText}>
    Development mode is enabled, your app will be slower but you can use useful development
    tools. {learnMoreButton}
    </Text>
    );
    } else {
    return (
    <Text style={styles.developmentModeText}>
    You are not in development mode, your app will run at full speed.
    </Text>
    );
    }
    }









    share|improve this question

























      0












      0








      0








      I am brand new to React Native and have just figured out setting up a local server and db for my native app. I am using express and mongodb. I have my server and mongo running on my backend and am trying to make an axios call to get the data from my database but the array is coming back empty and the response from the data is undefined. I have tested the endpoints on postman and the endpoints are fine. Here is an example of my api axios request and the component I am trying to get the data back in.
      Thank you in advanced for your help! Also if you would like to see any other code let me know and I will post it.



      // api.js

      import axios from "axios";

      export const getAllTweets = async () => {
      await axios
      .get("http://(my ip address):(port number)/api/tweet")
      .then(response => {
      console.log("response: ", response);
      return response.data;
      })
      .catch(error => {
      console.log("There are no tweets to get!", error);
      });
      };





      //HomeScreen.js

      import React from 'react';
      import {
      Image,
      Platform,
      ScrollView,
      StyleSheet,
      Text,
      TouchableOpacity,
      View,
      } from 'react-native';
      import { WebBrowser } from 'expo';

      import { getAllTweets } from '../constants/api';

      import { MonoText } from '../components/StyledText';

      export default class HomeScreen extends React.Component {
      static navigationOptions = {
      header: null,
      };
      static defaultProps = {
      getAllTweets
      };
      state = {
      tweets: ,
      isLoading: false
      }

      async componentDidMount() {
      this.setState({ isLoading: true });
      let data = await this.props.getAllTweets();
      console.log('data: ', data);
      try {
      this.setState({ isLoading: false, tweets: data });
      } catch (error) {
      console.log(error, "Cannot get tweets in cdm");
      }
      }

      render() {
      console.log("this.state.tweets", this.state.tweets); <--- this is empty
      return (
      <View style={styles.container}>

      </View>
      );
      }

      _maybeRenderDevelopmentModeWarning() {
      if (__DEV__) {
      const learnMoreButton = (
      <Text onPress={this._handleLearnMorePress} style={styles.helpLinkText}>
      Learn more
      </Text>
      );

      return (
      <Text style={styles.developmentModeText}>
      Development mode is enabled, your app will be slower but you can use useful development
      tools. {learnMoreButton}
      </Text>
      );
      } else {
      return (
      <Text style={styles.developmentModeText}>
      You are not in development mode, your app will run at full speed.
      </Text>
      );
      }
      }









      share|improve this question














      I am brand new to React Native and have just figured out setting up a local server and db for my native app. I am using express and mongodb. I have my server and mongo running on my backend and am trying to make an axios call to get the data from my database but the array is coming back empty and the response from the data is undefined. I have tested the endpoints on postman and the endpoints are fine. Here is an example of my api axios request and the component I am trying to get the data back in.
      Thank you in advanced for your help! Also if you would like to see any other code let me know and I will post it.



      // api.js

      import axios from "axios";

      export const getAllTweets = async () => {
      await axios
      .get("http://(my ip address):(port number)/api/tweet")
      .then(response => {
      console.log("response: ", response);
      return response.data;
      })
      .catch(error => {
      console.log("There are no tweets to get!", error);
      });
      };





      //HomeScreen.js

      import React from 'react';
      import {
      Image,
      Platform,
      ScrollView,
      StyleSheet,
      Text,
      TouchableOpacity,
      View,
      } from 'react-native';
      import { WebBrowser } from 'expo';

      import { getAllTweets } from '../constants/api';

      import { MonoText } from '../components/StyledText';

      export default class HomeScreen extends React.Component {
      static navigationOptions = {
      header: null,
      };
      static defaultProps = {
      getAllTweets
      };
      state = {
      tweets: ,
      isLoading: false
      }

      async componentDidMount() {
      this.setState({ isLoading: true });
      let data = await this.props.getAllTweets();
      console.log('data: ', data);
      try {
      this.setState({ isLoading: false, tweets: data });
      } catch (error) {
      console.log(error, "Cannot get tweets in cdm");
      }
      }

      render() {
      console.log("this.state.tweets", this.state.tweets); <--- this is empty
      return (
      <View style={styles.container}>

      </View>
      );
      }

      _maybeRenderDevelopmentModeWarning() {
      if (__DEV__) {
      const learnMoreButton = (
      <Text onPress={this._handleLearnMorePress} style={styles.helpLinkText}>
      Learn more
      </Text>
      );

      return (
      <Text style={styles.developmentModeText}>
      Development mode is enabled, your app will be slower but you can use useful development
      tools. {learnMoreButton}
      </Text>
      );
      } else {
      return (
      <Text style={styles.developmentModeText}>
      You are not in development mode, your app will run at full speed.
      </Text>
      );
      }
      }






      mongodb express react-native






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 22:27









      Jacob BralishJacob Bralish

      15119




      15119
























          2 Answers
          2






          active

          oldest

          votes


















          0














          It looks like you are mixing two different ways of calling async functions. Either you should be using async/await or a promise chain. You wouldn't do both.



          If you change your getAllTweets function to something like this.



          export const getAllTweets = async () => {
          // you need a try/catch as the await function can throw
          try {
          let response = await axios.get("http://(my ip address):(port number)/api/tweet");
          console.log("response: ", response);
          return response.data;
          } catch (error) {
          console.warn(error)
          return ;
          }
          };





          share|improve this answer
























          • Thank you for the response! I had tried something similar I must have missed something because I am no longer getting the data as undefined, but I am still unable to get the data out of the database, It is just returning the empty array. Also it is now returning this [22:48:35] Network Error - node_modules/axios/lib/core/createError.js:16:24 in createError - node_modules/axios/lib/adapters/xhr.js:87:25 in handleError - ... 9 more stack frames from framework internals Any ideas?

            – Jacob Bralish
            Jan 4 at 5:48





















          0














          I found my answer. The request was not coming through with the standard ip address. By going into system preferences => network => TCP/IP (tab) and using the IPv4 ip address it corrected the issue. It also works by providing it the ip address of the virtual router 10.0.2.2 that the emulator runs on.






          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%2f54030670%2fcannot-fetch-data-from-mongo-db-react-native%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














            It looks like you are mixing two different ways of calling async functions. Either you should be using async/await or a promise chain. You wouldn't do both.



            If you change your getAllTweets function to something like this.



            export const getAllTweets = async () => {
            // you need a try/catch as the await function can throw
            try {
            let response = await axios.get("http://(my ip address):(port number)/api/tweet");
            console.log("response: ", response);
            return response.data;
            } catch (error) {
            console.warn(error)
            return ;
            }
            };





            share|improve this answer
























            • Thank you for the response! I had tried something similar I must have missed something because I am no longer getting the data as undefined, but I am still unable to get the data out of the database, It is just returning the empty array. Also it is now returning this [22:48:35] Network Error - node_modules/axios/lib/core/createError.js:16:24 in createError - node_modules/axios/lib/adapters/xhr.js:87:25 in handleError - ... 9 more stack frames from framework internals Any ideas?

              – Jacob Bralish
              Jan 4 at 5:48


















            0














            It looks like you are mixing two different ways of calling async functions. Either you should be using async/await or a promise chain. You wouldn't do both.



            If you change your getAllTweets function to something like this.



            export const getAllTweets = async () => {
            // you need a try/catch as the await function can throw
            try {
            let response = await axios.get("http://(my ip address):(port number)/api/tweet");
            console.log("response: ", response);
            return response.data;
            } catch (error) {
            console.warn(error)
            return ;
            }
            };





            share|improve this answer
























            • Thank you for the response! I had tried something similar I must have missed something because I am no longer getting the data as undefined, but I am still unable to get the data out of the database, It is just returning the empty array. Also it is now returning this [22:48:35] Network Error - node_modules/axios/lib/core/createError.js:16:24 in createError - node_modules/axios/lib/adapters/xhr.js:87:25 in handleError - ... 9 more stack frames from framework internals Any ideas?

              – Jacob Bralish
              Jan 4 at 5:48
















            0












            0








            0







            It looks like you are mixing two different ways of calling async functions. Either you should be using async/await or a promise chain. You wouldn't do both.



            If you change your getAllTweets function to something like this.



            export const getAllTweets = async () => {
            // you need a try/catch as the await function can throw
            try {
            let response = await axios.get("http://(my ip address):(port number)/api/tweet");
            console.log("response: ", response);
            return response.data;
            } catch (error) {
            console.warn(error)
            return ;
            }
            };





            share|improve this answer













            It looks like you are mixing two different ways of calling async functions. Either you should be using async/await or a promise chain. You wouldn't do both.



            If you change your getAllTweets function to something like this.



            export const getAllTweets = async () => {
            // you need a try/catch as the await function can throw
            try {
            let response = await axios.get("http://(my ip address):(port number)/api/tweet");
            console.log("response: ", response);
            return response.data;
            } catch (error) {
            console.warn(error)
            return ;
            }
            };






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 3 at 23:37









            AndrewAndrew

            6,74241528




            6,74241528













            • Thank you for the response! I had tried something similar I must have missed something because I am no longer getting the data as undefined, but I am still unable to get the data out of the database, It is just returning the empty array. Also it is now returning this [22:48:35] Network Error - node_modules/axios/lib/core/createError.js:16:24 in createError - node_modules/axios/lib/adapters/xhr.js:87:25 in handleError - ... 9 more stack frames from framework internals Any ideas?

              – Jacob Bralish
              Jan 4 at 5:48





















            • Thank you for the response! I had tried something similar I must have missed something because I am no longer getting the data as undefined, but I am still unable to get the data out of the database, It is just returning the empty array. Also it is now returning this [22:48:35] Network Error - node_modules/axios/lib/core/createError.js:16:24 in createError - node_modules/axios/lib/adapters/xhr.js:87:25 in handleError - ... 9 more stack frames from framework internals Any ideas?

              – Jacob Bralish
              Jan 4 at 5:48



















            Thank you for the response! I had tried something similar I must have missed something because I am no longer getting the data as undefined, but I am still unable to get the data out of the database, It is just returning the empty array. Also it is now returning this [22:48:35] Network Error - node_modules/axios/lib/core/createError.js:16:24 in createError - node_modules/axios/lib/adapters/xhr.js:87:25 in handleError - ... 9 more stack frames from framework internals Any ideas?

            – Jacob Bralish
            Jan 4 at 5:48







            Thank you for the response! I had tried something similar I must have missed something because I am no longer getting the data as undefined, but I am still unable to get the data out of the database, It is just returning the empty array. Also it is now returning this [22:48:35] Network Error - node_modules/axios/lib/core/createError.js:16:24 in createError - node_modules/axios/lib/adapters/xhr.js:87:25 in handleError - ... 9 more stack frames from framework internals Any ideas?

            – Jacob Bralish
            Jan 4 at 5:48















            0














            I found my answer. The request was not coming through with the standard ip address. By going into system preferences => network => TCP/IP (tab) and using the IPv4 ip address it corrected the issue. It also works by providing it the ip address of the virtual router 10.0.2.2 that the emulator runs on.






            share|improve this answer




























              0














              I found my answer. The request was not coming through with the standard ip address. By going into system preferences => network => TCP/IP (tab) and using the IPv4 ip address it corrected the issue. It also works by providing it the ip address of the virtual router 10.0.2.2 that the emulator runs on.






              share|improve this answer


























                0












                0








                0







                I found my answer. The request was not coming through with the standard ip address. By going into system preferences => network => TCP/IP (tab) and using the IPv4 ip address it corrected the issue. It also works by providing it the ip address of the virtual router 10.0.2.2 that the emulator runs on.






                share|improve this answer













                I found my answer. The request was not coming through with the standard ip address. By going into system preferences => network => TCP/IP (tab) and using the IPv4 ip address it corrected the issue. It also works by providing it the ip address of the virtual router 10.0.2.2 that the emulator runs on.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 4 at 6:26









                Jacob BralishJacob Bralish

                15119




                15119






























                    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%2f54030670%2fcannot-fetch-data-from-mongo-db-react-native%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

                    Mossoró

                    Error while reading .h5 file using the rhdf5 package in R

                    Pushsharp Apns notification error: 'InvalidToken'