cookies are returning undefined

js-cookie
jquery cookie
document.cookie is empty
js-cookie cookies get returns undefined
react-cookie
javascript get cookie
document.cookie not working
javascript check if cookie exists

So i'm trying a simple cookie exercise and when i hit submit it returns undefined. Also is there a way to check what in a cookie in visual studio code?

function setcookie()
{
   var tmr = new Date();
    tmr.setDate(tmr.getDate() + 1);
    document.cookie = "username="+ document.getElementById("username").innerHTML +";path=/";
    document.cookie = "password="+ document.getElementById("password").value +";path=/";
}

function getcookie()
{
   var Carray = document.cookie.split(";");


   for(i = 0 ; i < Carray.length ; i++)
   {
        var valuearray =Carray[i].split("=")


        if (valuearray == "username")
        {
            var name = valuearray[1];
        }
        else if (valuearray == "password")
        {
            var password = valuearray[1];
        }
   }
   alert("username is " + name +" password is " + password);

}

valuearray will be a array, so you need to check valuearray[0] in the if condition.

var valuearray = Carray[i].split("=")
if (valuearray[0] == "username"){ //Here, valuearray[0]
  var name = valuearray[1];
} else if (valuearray[0] == "password"){
  var password = valuearray[1];
}

Cookie returns undefined value in JavaScript, In your case range is a span not an input element, so you need to use innerHTML property to read the contents of that element instead of using� So the problem here is occasionally, that undefined block is being tripped. We know the cookie is being set by the server so the issue isn't on our end. We've noticed that it happens most frequently with mobile users using in-app browsers (such as hitting the page from a Facebook share link, inside the Facebook App).

Try this

function getcookie() {
  const Carray = document.cookie.split(";");
  const [name, password] = Carray.map(item => item.split('=')[1])
  console.log("username is " + name +" password is " + password);
}

or if you want to transform your cookies into object use this code

function getcookie()
{
   var Carray = document.cookie.split(";");
   var cookieObj = Carray.reduce((cookieObj, current) => {
    const [key, value] = current.split('=');
    return { ...cookieObj, ...{ [key]: value } }
   }, {})
   console.log("username is " + cookieObj.username +" password is " + cookieObj.password);
   return cookieObj;
}

// result
{
  username: 'usersname'
  password: 'userspassword'
}

Please return your result from the functions, I'm not returning anything because I'm trying to match your code.

and answer to your second question. yes you can get value from vscode but for that you need to start your debugging mode

Koa cookie returning `undefined` : node, Koa cookie returning `undefined`. After a POST request is sent from the browser to the /generate url in the server, I want to create a string and save it as a cookie. Hi there, I've recently hit a related issue - if I have the cookie name in a variable, and in some situations I need to be able to handle where that variable is not set, or it has a default value like null or empty string, in those cases it makes more sense to me for get to return null instead of all of the cookies. As the op says, if I pass in

name and password are declared in the block of for, but you use them in out of the block. change getcookie function to this:

var name="";
var password ="";
for(i = 0 ; i < Carray.length ; i++)
   {
        var valuearray =Carray[i].split("=") 
        if (valuearray == "username")
        {
            name = valuearray[1];
        }
        else if (valuearray == "password")
        {
            password = valuearray[1];
        }
   }
   alert("username is " + name +" password is " + password);

Cookies, document.cookie, getToken = function() { return $cookies.USER_SESSION; }; However it always returns undefined even when the USER_SESSION cookie exists in the browser. The documentation states that a missing cookie will return undefined. This seems to be incorrect, as null is returned when the cookie is not found. Perhaps the documentation can be updated?

ngCookies returns undefined for existing cookie - angularjs - html, cookieStore.get() return undefined in angularjs - angularjs. Edit: I tried with $ cookies service and I get undefined as well. I send the cookie in the server side� Cookies are small strings of data that are stored directly in the browser. They are a part of HTTP protocol, defined by RFC 6265 specification.. Cookies are usually set by a web-server using response Set-Cookie HTTP-header.

$cookieStore.get() return undefined in angularjs - angularjs - html, If no matching cookie could be found, null is returned. This is an asynchronous function that returns a Promise . Syntax. var getting = browser. Dismiss Join GitHub today. GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.

cookies.get(), The Document property cookie lets you read and write cookies //ES5 if ( document.cookie.split(';').some(function(item) { return item.trim(). myVariable is accessible and contains undefined even before the declaration line: var myVariable = 'Initial value'.. Contrary, a const or let variable cannot be accessed before the declaration line — the variable is in a temporal dead zone before the declaration.

Comments
  • Can you share the HTML part?
  • it still come up as undefined even after i changed it to an array, could it be an input issue?
  • i'm dumb and my id is different from my elementId, thank you so much for your help