5

I'm using this library for a JQuery plugin I'm coding... I save inside a cookie a specific data created by the user in this way:

// Update cookies checks if the cookie esists.
// If cookie exists => push data {myData:[obj1[, obj2, [...], objN]]}
// If cookie doesn't exists => create cookie with {myData:[obj1]}
function _updateCookie(name, cookie, data) {
    // Check if cookie exists
    var cookies = {myData:[]};

    // _getCookie(name) { return Cookies.get(name) }
    if (_getCookie(name) != undefined) {
        cookies = _getCookie(name);
    }

    cookies.reminders.push(cookie);

    Cookies.set(name, cookies, data);
}

DATA:

var data = {
    expires: 1,
    path: '/',
    domain: '',
    secure: true
}

COOKIE:

var cookie = {
    myData: [
        1: myObject1,
        2: myObject2,
        // [...]
        n: myObjectN
    ],
}

When I call _getCookie(name) it always returns undefined. I've also tried to list the cookies with:

console.log(Cookies.get());
// log: Object {_ga: "GA1.1.402426237.1450622600"}

But if i look at chrome://settings/cookies i see:

2 cookies on localhost [_ga], [myCookie]

Any suggestion of what am I doing wrong?

EDIT

I saw that the problem comes out when i call Cookie.set(); with this values

Cookies.set('myCookie', {[expires: ...], path: '', domain: ''});

if I call

Cookies.set('myCookie'{[expires: ...]});

I can get the cookie with no problems.

IlGala
  • 3,331
  • 4
  • 35
  • 49
  • you had save cookie in Array and accessing only via name. You should access like `myData[0]` – Parth Trivedi Dec 20 '15 at 15:35
  • Sorry, I don't understand what you mean – IlGala Dec 20 '15 at 15:38
  • Can you provide how you are pushing your cookie? what data you want to push and access? – Parth Trivedi Dec 20 '15 at 15:40
  • the `_updateCookie()` function checks at first time if the cookie exists, if it doesn't it creates it like: Cookies.set('myCookie', {myData:[array]}, {expires: 1, secure: true, path: '/', domain: ''}); – IlGala Dec 20 '15 at 15:41
  • I'm editing also the question – IlGala Dec 20 '15 at 15:43
  • try `Cookies.getJSON()` to get cookie. what are you getting from this? – Parth Trivedi Dec 20 '15 at 15:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98480/discussion-between-parth-trivedi-and-ilgala). – Parth Trivedi Dec 20 '15 at 15:46
  • You have to do JSON.stringify on your cookie Array before storing. May be this will help you http://stackoverflow.com/questions/8163815/cookie-array-in-javascript?answertab=active#tab-top – Parth Trivedi Dec 20 '15 at 16:18
  • @ParthTrivedi please check github... **This project was moved to https://github.com/js-cookie/js-cookie, check the discussion. New issues should be opened at https://github.com/js-cookie/js-cookie/issues** I'm using the new project, the param $.cookie.json and JSON.stringify have been removed... – IlGala Dec 20 '15 at 16:21

0 Answers0