Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing the cookie header or object #84

Closed
davidsonsns opened this issue May 25, 2017 · 26 comments
Closed

Missing the cookie header or object #84

davidsonsns opened this issue May 25, 2017 · 26 comments

Comments

@davidsonsns
Copy link

I am using universal-cookie and am experiencing this problem when instantiating it. I'm doing the same thing as the example:

import Cookies from 'universal-cookie';
const cookies = new Cookies();
@garythung
Copy link

Running into this issue as well. universal-cookie seems to think I'm using this on the server side but it's on client side.

@davidsonsns
Copy link
Author

I returned to the version I was using the longest, "react-cookie": "^ 0.4.8"

@eXon
Copy link
Collaborator

eXon commented May 26, 2017

With what bundler are you using this library? It seems like it is mistakingly thinking you are in a Node.js environment.

@garythung
Copy link

garythung commented May 26, 2017

I'm using Webpack with React and universal routing, which I think is the problem. Should I be using react-cookie instead?

For the client side, I'm trying to write a helper module that will access cookies. This helper will be used in my React components.

@eXon
Copy link
Collaborator

eXon commented May 27, 2017

If you care about async server-rendering you should definitively use react-cookie instead. However, universal-cookie should be working on Webpack. I'll take a look.

@Brantron
Copy link

Currently having this issue when running tests as well.

@eXon
Copy link
Collaborator

eXon commented May 27, 2017

@Brantron when testing, you might want to mock your cookies and passing it to the Cookies constructor. But yea I kinda agree it should not block you from not donig so in Node.js. I'm trying to look if removing this check has an impact on other parts.

@eXon
Copy link
Collaborator

eXon commented May 31, 2017

@garythung @davidsonsns I've fixed this issue in v2.0.8. Can you confirm you no longer have this issue?

@andrefox333
Copy link

Using 2.0.8 and getting error: Error: Missing the cookie header or object.

Using universal-cookie with server side rendering and hot reload. I would comment out the code, refresh the page and it loads the page. I'll then uncomment the new Cookie() code and reload the page and it fully works after that...

@eXon
Copy link
Collaborator

eXon commented Jun 1, 2017

@andrefox333 this is because you are doing server-rendering but you are not providing the cookie header or object to the library. Thus, the cookies will be empty even if the user have some. You need to provide them in some way using universal-cookie-express and react-cookie or using your own custom way.

Would you mind sharing your code or the portion dealing with cookies and SSR?

@garythung
Copy link

garythung commented Jun 1, 2017

@eXon I appreciate the work you've put in. Unfortunately, I've moved from universal rendering to just client side, so the universal-cookie is working great for me and I can't help debug any universal rendering issues.

@kunokdev
Copy link

kunokdev commented Jun 13, 2017

I came across this issue when using Next.js for server side rendering.

My solution was:
Instead of initializing object from class in global scope, I moved it to componentDidMount and this error was gone, hope it helps!

  componentDidMount(){
    const cookies = new Cookies()
    ...
  }

@tamal-appsbee
Copy link

Still facing the same issue. Any solution ?

@kunokdev
Copy link

@tamal-appsbee are you server side rendering? can u show your code where u get error?

@tamal-appsbee
Copy link

tamal-appsbee commented Jun 16, 2017

@kunokdev thanks for reply.
Yes I am using server side rendering.
I have a file name : cookie.js has following content

import Cookies from 'universal-cookie';
export const cookies = new Cookies();

In store using following:

import {cookies} from 'common/util/cookies';
....
const cookieData = cookies.get('name') || {};
  const store = createStore(
    RootReducer,
    cookieData,
    applyMiddleware(routemiddleware, sagaMiddleware)
  );

@kunokdev
Copy link

kunokdev commented Jun 16, 2017

you are getting error at this line; export const cookies = new Cookies();
Since you are server side rendering, constructor of Cookies class gets broken because it excepts browser environment. Basically you need to make sure you call this constructor when it reaches client side. One way to do that is to put that logic inside componentDidMount, see comment above. Downside of this approach is that I had to initialize this object in more than just one place if I wanted to use it outside of componentDidMount.

@tamal-appsbee
Copy link

tamal-appsbee commented Jun 16, 2017

Ok,
Otherwise I shall use
const cookies = new Cookies(req.headers.cookie);
this way in express and can utilize current code ?

@kunokdev
Copy link

kunokdev commented Jun 16, 2017

I haven't used this approach, to me it seems it would be used inside routers where req is available. As for React Components you should use cookie middleware to merge these scopes. I haven't succeed at that yet, so I use componentDidMount workaround.

@tamal-appsbee
Copy link

ok, Thanks

@Brantron
Copy link

Just to loop back, our solution was to just stub this object.

@andrefox333
Copy link

@Brantron can you provide a code example of what you described above?

@Brantron
Copy link

Brantron commented Jun 29, 2017

Sorry @andrefox333 ! That would certainly have been helpful. We wrapped our cookie usage in a helper object and just stubbed access like

sinon.stub(CookieHelpers, 'getUserStatus').returns({
    signedIn: true,
    mentor: true
  });
`

@eXon eXon closed this as completed Jul 2, 2017
@mmoustai
Copy link

mmoustai commented Jul 7, 2017

@eXon I am doing server side rendering and I would like to know if it's possible to get instance of the cookie object outside a component. I am getting the same errors as @andrefox333.

I have a single util function and I want to know if I need to give the cookie from the component as a new argument at every call or I might be able to access it as a "global".

@eXon
Copy link
Collaborator

eXon commented Jul 8, 2017

@mmoustai You must pass the Cookies object as an argument. You cannot use a global variable otherwise you risk hijacking the cookies from another user.

@mahdieh-dev
Copy link

still having this issue. cookies.get() works as long as I haven't reloaded the page after setting cookie, but when I reload the page, it returns: undedined

@eXon
Copy link
Collaborator

eXon commented Oct 27, 2020

@mshavandi Do you have a step by step or repo to reproduce the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants