# CORS Error when running App on Real Device

**URL:** <https://forum.ionicframework.com/t/cors-error-when-running-app-on-real-device/160149>\
**Category:** Ionic Framework\
**Created:** [March 27, 2019, 6:20pm UTC](https://forum.ionicframework.com/t/cors-error-when-running-app-on-real-device/160149 "2019-03-27T18:20:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmcgowan94](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@bmcgowan94](https://forum.ionicframework.com/u/bmcgowan94)\
**Post date:** [March 27, 2019, 6:20pm UTC](https://forum.ionicframework.com/t/cors-error-when-running-app-on-real-device/160149/1 "2019-03-27T18:20:54Z")

</div>

Hi guys, Im using a firebase cloud function (code shown below) which basically updates the number of likes on a particular post (its for a social media app).

```auto
/* this cloud function is used to update the likes on a post
- it receives three parameters in the post body 
- and will update those respective fields in the post document (e.g number of likes)
*/

export const updateLikesCount = functions.https.onRequest((request, response) => {

  const postId = JSON.parse(request.body).postId;
  const userId = JSON.parse(request.body).userId;
  const action = JSON.parse(request.body).action; // 'like' or 'unlike' 

  // from this data object we can get the value of all the fields in the document
  admin.firestore().collection("posts").doc(postId).get().then((data) => {

    // if likes counts = 0, then assume there are no likes 
    let likesCount = data.data().likesCount || 0;
    // likewise if there are no likes, then assign an empty array
    let likes = data.data().likes || [];

    let updateData = {};

    if(action == "like")
    {
      updateData["likesCount"] = ++likesCount;
      updateData[`likes.${userId}`] = true;
    } 
    else 
    {
      updateData["likesCount"] = --likesCount;
      updateData[`likes.${userId}`] = false;
    }

    admin.firestore().collection("posts").doc(postId).update(updateData).then(() => {
      response.status(200).send("Done")
    }).catch((err) => {
      response.status(err.code).send(err.message);
    })

  }).catch((err) => {
    response.status(err.code).send(err.message);
  })

})

```

When the app is running and the ‘like’ button is pressed, I get this console error…

 ![47](https://global.discourse-cdn.com/ionicframework/original/3X/7/9/79b05c2b5d35dcc65a05873f7d0daf230bbc9cc0.png)

Please help, I cant seem to find many answers online, and the solutions I do find don’t seem to work for me. Many thanks! 🙂

---

<div class="post-metadata">

**Author:** ![bandito](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bandito/32/94980_2.png) [@bandito](https://forum.ionicframework.com/u/bandito)\
**Post date:** [March 27, 2019, 11:39pm UTC](https://forum.ionicframework.com/t/cors-error-when-running-app-on-real-device/160149/2 "2019-03-27T23:39:25Z")

</div>

Are you using Cordova?

Add this to the config.xml:

`access origin="*"`

And this plugin: cordova-plugin-whitelist

[https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/index.html](https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/index.html)

---

<div class="post-metadata">

**Author:** ![bmcgowan94](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@bmcgowan94](https://forum.ionicframework.com/u/bmcgowan94)\
**Post date:** [March 28, 2019, 11:11am UTC](https://forum.ionicframework.com/t/cors-error-when-running-app-on-real-device/160149/3 "2019-03-28T11:11:27Z")

</div>

Thanks for the reply Bandito,

Yes I am using Cordova, and have installed the plugin you mentioned.

I also seem to already have that line of code in the file… as shown below…

![image](https://global.discourse-cdn.com/ionicframework/original/3X/0/8/082645f5357877fe4f97b7cbae423dbde0a7fb0c.png)

Below I have included a screenshot of the console log for error info. The weird thing is that the likes actually do get updated in Firebase, but I still get an error message on the console due to CORS…

 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/5/0/50c96f9d6eb1f92b04e584bf26998718b536f658.jpeg)

---

<div class="post-metadata">

**Author:** ![bandito](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bandito/32/94980_2.png) [@bandito](https://forum.ionicframework.com/u/bandito)\
**Post date:** [March 28, 2019, 12:13pm UTC](https://forum.ionicframework.com/t/cors-error-when-running-app-on-real-device/160149/4 "2019-03-28T12:13:14Z")

</div>

Oh I think that happened to me. I think it was an Angular thing, I think I fixed it because my server wasn’t accepting any connections because I didn’t include the local host/IP.

But it was a server issue not an Ionic. Check that the GET function have the correct headers. That was I think I did the fix the issues.

I think for Firebase all you need is the API key

---

<div class="post-metadata">

**Author:** ![bmcgowan94](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@bmcgowan94](https://forum.ionicframework.com/u/bmcgowan94)\
**Post date:** [March 28, 2019, 3:37pm UTC](https://forum.ionicframework.com/t/cors-error-when-running-app-on-real-device/160149/5 "2019-03-28T15:37:28Z")

</div>

I found the solution which worked for me bandito, great article describing the solution here…

> **[Enabling CORS Middleware In Firebase Cloud Functions - I Like Kill Nerds](https://ilikekillnerds.com/2017/05/enabling-cors-middleware-firebase-cloud-functions/)**
>
> Firebase Cloud Functions are great, but there might come a time where you need CORS support. This can be enabled easily by using the CORS middleware. The documentation does detail part of the process, but it doesn’t mention you need to install the...

So here is my updated cloud function for anyone experiencing similar issues…

```auto
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp(functions.config().firebase);

const cors = require('cors')({
  origin: true
});

/* this cloud function is used to update the likes on a post
- it receives three parameters in the post body 
- and will update those respective fields in the post document (e.g number of likes)
- check out this link for work around for CORS error...
- https://ilikekillnerds.com/2017/05/enabling-cors-middleware-firebase-cloud-functions/
*/
exports.updateLikesCount = functions.https.onRequest((req, res) => {
  cors(req, res, () => {
     
      console.log(req.body);
    
      const postId = JSON.parse(req.body).postId;
      const userId = JSON.parse(req.body).userId;
      const action = JSON.parse(req.body).action; // 'like' or 'unlike' 
    
      // from this data object we can get the value of all the fields in the document
      admin.firestore().collection("posts").doc(postId).get().then((data) => {
    
        // if likes counts = 0, then assume there are no likes 
        let likesCount = data.data().likesCount || 0;
        // likewise if there are no likes, then assign an empty array
        let likes = data.data().likes || [];
    
        let updateData = {};
    
        if(action == "like")
        {
          updateData["likesCount"] = ++likesCount;
          updateData[`likes.${userId}`] = true;
        } 
        else 
        {
          updateData["likesCount"] = --likesCount;
          updateData[`likes.${userId}`] = false;
        }
    
        admin.firestore().collection("posts").doc(postId).update(updateData).then(() => {
          res.status(200).send("Done")
        }).catch((err) => {
          res.status(err.code).send(err.message);
        })
    
      }).catch((err) => {
        res.status(err.code).send(err.message);
      })
    
    })
  });

```

Thanks for your help! 🙂
