简体   繁体   English

调用get()时reactjs通用cookie包错误

[英]reactjs universal-cookie package error when calling get()

I have the following two components.我有以下两个组件。 They are in separate .js files.它们位于单独的 .js 文件中。

import Cookie from 'universal-cookie'
class Comp1{

   someFunction(){
      // Call google Oauth
      // get userinfo from Oauth response and set the token below
      var cookie = New Cookie();
      cookies.set('user', user, { path: '/' })
      this.props.history.push('/Comp2')
      window.location.reload()
   }

}

--------------------------------------------------------- -------------------------------------------------- -------

import cookie from 'universal-cookie'
class Comp2{

   anotherFunction(){
       var user = cookie.get('user');
   }

}

The second component throws the following error:第二个组件抛出以下错误:

TypeError: universal_cookie__WEBPACK_IMPORTED_MODULE_4__.default.get is not a function

How do I resolve it?我该如何解决?

UPDATE: google oauth flow更新:谷歌 oauth 流程

I think the problem is that you are not calling new Cookie();我认为问题在于您没有调用new Cookie(); in the second block of code, hence the methods like get and set are not initialized.在第二个代码块中,因此像 get 和 set 这样的方法没有被初始化。

You can try this approach where you call new every time you want to access the cookies.您可以尝试这种方法,每次要访问 cookie 时都调用 new。

import Cookie from 'universal-cookie'
class Comp2 {
   anotherFunction(){
       var cookie = New Cookie();
       var user = cookie.get('user');
   }
}

Or you could create a "global" variable that you access from anywhere.或者您可以创建一个可以从任何地方访问的“全局”变量。 Because you are accessing the globalCookie in separate files you would have to put the initializing code in a file that are called/imported before the files that contain your code that uses the globalCookie variable.因为您在单独的文件中访问globalCookie ,所以您必须将初始化代码放在一个文件中,该文件在包含使用globalCookie变量的代码的文件之前调用/导入。 But please be very careful using globals as they are a easy way to introduce hard to find bugs in to your code.但是请非常小心地使用全局变量,因为它们很容易将难以发现的错误引入到您的代码中。

import Cookie from 'universal-cookie';
var globalCookie = New Cookie();

class Comp1{
   someFunction(){
      globalCookie.set('user', user, { path: '/' })
   }
}
class Comp2{
   anotherFunction(){
       var user = globalCookie.get('user');
   }
}

See github repo for docs: https://github.com/reactivestack/cookies/tree/master/packages/universal-cookie有关文档,请参阅 github 存储库: https : //github.com/reactivestack/cookies/tree/master/packages/universal-cookie

You have import cookie from 'universal-cookie' in your Comp2.您在 Comp2 中import cookie from 'universal-cookie' Replace it with import Cookie from 'universal-cookie'将其替换为import Cookie from 'universal-cookie'

Firstly, var cookie = New Cookie();首先, var cookie = New Cookie(); will cause syntax errors.会导致语法错误。

Use var cookie = new Cookie();使用var cookie = new Cookie(); as JavaScript keywords as key sensitive.作为 JavaScript 关键字作为关键敏感。

Secondly, you are inconsistent with your case-sensitivity again with your imports:其次,您的导入再次与区分大小写不一致:

import Cookie from 'universal-cookie'从 'universal-cookie' 导入 Cookie

import cookie from 'universal-cookie'从 'universal-cookie' 导入 cookie

These will cause confusion.这些都会造成混乱。 Your going to want to refactor that to standardize across the components.您将要重构它以在组件之间标准化。 Example just opt with the capital 'C':示例只需选择大写字母“C”:

import Cookie from 'universal-cookie'

Lastly, taking into consideration the case-sensitivity I have mentioned & as others have mentioned, you need to instantiate Cookie as it is a class .最后,考虑到我提到的区分大小写和其他人提到的,您需要实例化 Cookie 因为它是一个类 Here is how your components should look like:以下是您的组件的外观:

import Cookie from 'universal-cookie';

class Comp extends React.Component {
  ...
  someFunction() {
    var cookie = new Cookie();
    cookie.set('user', "somecookie", { path: '/' })
  }
  ...
}

import Cookie from 'universal-cookie'

class Comp extends React.Component {
    ...
    someFunction() {
        var cookie = new Cookie();
        console.log(cookie.get('user'));
    }
    ...
}

CodeSandBox: https://codesandbox.io/s/universal-cookie-react-components-8lc3m CodeSandBox: https ://codesandbox.io/s/universal-cookie-react-components-8lc3m

Also, just FYI don't use Incognito for testing my sandbox另外,仅供参考,不要使用隐身模式来测试我的沙箱

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM