title: Unhandled promise rejection: TypeError: Network request failed in expo react native
body: <p>I am creating a MERN react native mobile apps using expo but I am stuck on how to connect the REST API using express. Below are the code.</p>
  <blockquote>
  <p><strong>App.js</strong></p>
  </blockquote>
  <pre><code>import React from &quot;react&quot;;
  import {
     StyleSheet,
     Text,
     View,
     TextInput,
     TouchableOpacity,
     SafeAreaView,
  } from &quot;react-native&quot;;
  
  class Form extends React.Component {
     constructor() {
     super();
     this.State = {
        title: &quot;&quot;,
        description: &quot;&quot;,
     };
  }
  
  getInput(text, field) {
     if (field == &quot;title&quot;) {
        this.setState({ title: text });
     } else if (field == &quot;description&quot;) {
       this.setState({ description: text });
     }
     //console.warn(text)
   }
   submit() {
     let collection = {};
     (collection.title = this.state.title),
     (collection.description = this.state.description);
      console.warn(collection);
     var url = &quot;http://localhost/3000&quot;;
     fetch(url, {
        method: &quot;POST&quot;,
        headers: {
           Accept: &quot;application/json&quot;,
           &quot;Content-Type&quot;: &quot;application/json&quot;,
        },
        body: JSON.stringify({
        collection,
       }),
     });
   }
  
   render() {
     return (
       &lt;SafeAreaView style={styles.container}&gt;
       &lt;TextInput
        underlineColorAndroid=&quot;rgba(0,0,0,0)&quot;
        placeholder=&quot;Title&quot;
        selectionColor=&quot;#fff&quot;
        keyboardType=&quot;default&quot;
        onChangeText={(text) =&gt; this.getInput(text, &quot;title&quot;)}
       /&gt;
  
      &lt;TextInput
        multiline={true}
        numberOfLines={4}
        underlineColorAndroid=&quot;rgba(0,0,0,0)&quot;
        placeholder=&quot;Description&quot;
        selectionColor=&quot;#fff&quot;
        keyboardType=&quot;default&quot;
        onChangeText={(text) =&gt; this.getInput(text, &quot;description&quot;)}
      /&gt;
  
      &lt;TouchableOpacity onPress={() =&gt; this.submit()}&gt;
        &lt;Text&gt;Submit&lt;/Text&gt;
      &lt;/TouchableOpacity&gt;
    &lt;/SafeAreaView&gt;
   );
   }
  }
  export default Form;
  </code></pre>
  <blockquote>
  <p><strong>Post.js</strong></p>
  </blockquote>
  <pre><code>const mongoos = require(&quot;mongoose&quot;);
  
  const PostSchema = mongoos.Schema({
     title: {
        type: String,
        required: true,
     },
     description: {
        type: String,
        required: true,
     },
     date: {
        type: Date,
        default: Date.now,
     },
   });
  
   module.exports = mongoos.model(&quot;Post&quot;, PostSchema); // giving this schma name Post
  </code></pre>
  <p><strong>posts.js</strong></p>
  <pre><code>const express = require(&quot;express&quot;);
  const router = express.Router();
  const Post = require(&quot;./Post&quot;);
  
  //Gets back all the posts
  router.get(&quot;/&quot;, async (req, res) =&gt; {
     try {
        const post = await Post.find();
        res.json(post);
     }catch (err) {
        res.json({ message: err });
     }
  });
  
  //To Submit the Post
  router.post(&quot;/&quot;, async (req, res) =&gt; {
    //console.log(req.body);
    const post = new Post({
      title: req.body.title,
      description: req.body.description,
    });
    try {
      const savedPost = await post.save();
      res.json(savedPost);
    } catch (err) {
      res.json({ message: err });
    }
  });
  
  //Get back specific Post
  router.get(&quot;/:postId&quot;, async (req, res) =&gt; {
     try {
       const post = await Post.findById(req.params.postId);
       res.json(post);
       } catch (err) {
         res.json({ message: err });
     }
  });
  // to delete specific post
  router.delete(&quot;/:postId&quot;, async (req, res) =&gt; {
    try {
      const removePost = await Post.remove({ _id: req.params.postId });
      res.json(removePost);
    } catch (err) {
      res.json({ message: err });
    }
  });
  
  //update Post
  router.patch(&quot;/:postId&quot;, async (req, res) =&gt; {
     try {
       const updatePost = await Post.updateOne(
         { _id: req.params.postId },
         { $set: { title: req.body.title } }
       );
       res.json(updatePost);
     } catch (err) {
       res.json({ message: err });
     }
  });
  
  module.exports = router;
  </code></pre>
  <blockquote>
  <p><strong>server.js</strong>**</p>
  </blockquote>
  <pre><code>const express = require(&quot;express&quot;);
  const app = express();
  const mongoos = require(&quot;mongoose&quot;);
  const bodyParser = require(&quot;body-parser&quot;);
  const postRoute = require(&quot;./posts&quot;);
  
  const url = &quot;mongodb://localhost/REST_API&quot;;
  
  app.use(bodyParser.json());
  app.use(&quot;/post&quot;, postRoute);
  
  app.get(&quot;/&quot;, (req, res) =&gt; {
     res.send(&quot;We are on Home &quot;);
  });
  
  // connecting to database
  mongoos.connect(url, { useNewUrlParser: true });
  const con = mongoos.connection;
  
  con.on(&quot;open&quot;, () =&gt; {
     console.log(&quot;database connected,,,&quot;);
  });
  
  app.listen(3000);
  </code></pre>
  <blockquote>
  <p><strong>Below is the given error whenever I run it.</strong></p>
  </blockquote>
  <p>[Unhandled promise rejection: TypeError: Network request failed]
  at node_modules\whatwg-fetch\dist\fetch.umd.js:535:17 in setTimeout$argument_0
  at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer
  at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:383:16 in callTimers
  at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
  at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
  at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
  at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
  at [native code]:null in callFunctionReturnFlushedQueue</p>