1

This is the Code I use to create my cookie

 document.cookie = (firebase.auth().currentUser.phoneNumber);
    handleSignedInUser(user);

This is the code I use to read the value of my cookie and assign its value to a google map marker label

var markerLabel = document.cookie;
  var marker = new google.maps.Marker({
    position: {
      lat: data.User.l[0],
      lng: data.User.l[1]
      
    },
    map: map,
    label: markerLabel
  });

Picture of Homepage Instead of only displaying the phone number it also says phone number undefined. Another stack overflow user mentioned that Im not returning the value of the cookie correctly. I found this function for returning the value of a cookie on https://www.w3schools.com/js/js_cookies.asp

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}
I can't seem to figure out how to apply this function to my code. If anyone could help me with this I would really appreciate it
Community
  • 1
  • 1
BIlly BOB
  • 25
  • 4
  • Use setCookie function first to set cookie with proper name. Then access it with getCookie. – dfsq May 16 '18 at 06:30

1 Answers1

0

You need to set cookie properly with a name so that you can read it by name later. Where ever you found getCookie function, there should be an opposite one, something like setCookie which you should use to set cookie. So you workflow will be like this:

setCookie('phoneNumber', firebase.auth().currentUser.phoneNumber);

Then

var markerLabel = getCookie('phoneNumber');
var marker = new google.maps.Marker({
  position: {
    lat: data.User.l[0],
    lng: data.User.l[1]
  },
  map: map,
  label: markerLabel
});
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • Uncaught ReferenceError: setCookie is not defined at signInSuccess (auth.js:11) at xk (firebaseui.js:291) at wk (firebaseui.js:289) at firebaseui.js:323 – BIlly BOB May 16 '18 at 07:16
  • Of course it's not defined, as I said in my answer, you need to include it first same you did for getCookie. – dfsq May 16 '18 at 07:40
  • so should I make my document.cookie = setCookie('phoneNumber', firebase.auth().currentUser.phoneNumber); – BIlly BOB May 16 '18 at 07:45
  • No, you should add `setCookie` function same you did for getCookie. And set cookie `setCookie('phoneNumber', firebase.auth().currentUser.phoneNumber);`. Don't touch `document.cookie` manually. – dfsq May 16 '18 at 08:53
  • I don't have a get cookie function in my code. The code that I posted was a function I found online but I'm not sure how to implement it – BIlly BOB May 16 '18 at 19:07
  • One more time: if you want to use getCookie function, you need to include it into your project. Similarly you need setCookie to be included either. So take those two functions from here https://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie and use in your code. – dfsq May 16 '18 at 20:55