salemdar / angular2-cookie

Implementation of Angular 1.x $cookies service to Angular 2
MIT License
109 stars 29 forks source link

TypeError: newOpts.merge is not a function #39

Closed brendanalexdr closed 7 years ago

brendanalexdr commented 7 years ago

I'm using angular2-cookie v1.2.6. I am now getting the following error (was working in previous versions):

TypeError: newOpts.merge is not a function at CookieService._mergeOptions (cookies.service.js:194) at CookieService._buildCookieString (cookies.service.js:167) at cookies.service.js:152 at CookieService.put (cookies.service.js:83) at LoginControlComponent.saveToken (login-control.component.ts:105) at login-control.component.ts:76 at ZoneDelegate.invoke (zone.js:242) at Object.onInvoke (ng_zone.js:269) at ZoneDelegate.invoke (zone.js:241) at Zone.run (zone.js:113) at zone.js:520 at ZoneDelegate.invokeTask (zone.js:275) at Object.onInvokeTask (ng_zone.js:260) at ZoneDelegate.invokeTask (zone.js:274) at Zone.runTask (zone.js:151)

Here is the code:

`saveToken(token: string, loginEmail: string) { let dtm = new Date(); let minutes = 115; dtm.setTime(dtm.getTime() + (minutes 60 1000)); this.cookieService.put('tokenCookie', token, { expires: dtm, path: '/' });

    dtm = new Date();
    dtm = this.nk.dtm.addDays(dtm, 365);
    this.cookieService.put('loginEmailCookie', loginEmail, { expires: dtm, path: '/' });

}`

Is there something wrong with my code? Or is this a bug? Thank for any help on this.

exequiel09 commented 7 years ago

Same here on Angular 2.4.1. It works when not passing options as the 3rd argument of .put().

exequiel09 commented 7 years ago

This line causes the error.

exequiel09 commented 7 years ago

@nukuuk I think this is related to https://github.com/salemdar/angular2-cookie/issues/37. I think the culprit here is my temporary fix. Since we provided a blank object for the CookieOptions. It throws the error about .merge() is not a function and the class CookieOptions defines the .merge() function.

What do you think of this @salemdar ?

brendanalexdr commented 7 years ago

@exequiel09 I am using the fix as mentioned in #37 thread: { provide: CookieOptions, useValue: {} }

But still get this 'newOpts.merge is not a function' error. However, seems the culprit certainly is CookieOptions.

exequiel09 commented 7 years ago

@nukuuk Checkout the new solution on #37 but that will only work on JIT not on AOT.

Spawnrad commented 7 years ago

All solution Doesn't work for me. when i don't add options it work. when i add options it doesn't work. i don't use AOT.

have tried :

{ provide: CookieOptions, useValue: {} }

and :

import { BaseCookieOptions, CookieService, CookieOptions } from 'angular2-cookie/core'; { provide: CookieOptions, useClass: BaseCookieOptions }

Nothing work.

NgxDev commented 7 years ago

@Spawnrad Works fine for me, with Angular 2.4.8, BUT without a 3rd parameter for the .put method (so, this means not being able to set an expiration date): In app.module.ts:

import { CookieService, CookieOptions } from 'angular2-cookie/core';

// ...

providers: [
    // ...
    CookieService,
    { provide: CookieOptions, useValue: {} }
]

And simply inject CookieService in whatever component/service you want to use it, of course.

SyangC commented 7 years ago

@MrCroft Could I ask if there is another way to include expiration dates? Many thanks!

NgxDev commented 7 years ago

@SyangC Sorry, haven't figured out any, yet... Luckily for me, don't really need it now.

SyangC commented 7 years ago

Damn. Thanks anyways!

warerwang commented 7 years ago

@MrCroft @SyangC @Spawnrad I find a way. use { provide: CookieOptions, useValue: false }

because in angular2-cookie source code var defaultOpts = this._defaultOptions || new base_cookie_options_1.CookieOptions({ path: cookiePath }); var opts = this._mergeOptions(defaultOpts, options); so if this._defaultOptions is false. defaultOpts is the correct type

salemdar commented 7 years ago

Please use the new library guys:

https://github.com/salemdar/ngx-cookie

crufter commented 7 years ago

I get the same with the other library also when using AOT.

phil123456 commented 6 years ago

the new library introduced a new error and is not working with angular 5