简体   繁体   English

类型错误:react Js 中的预期参数 accessToken

[英]Type error: expected parameter accessToken in react Js

Error screenshot:错误截图:

While running react project by npm start , it shows error related to contentful.js .通过npm start运行反应项目时,它显示与contentful.js相关的错误。 why does the package shows these types of error?为什么 package 会显示这些类型的错误? I attached the screenshot with this post.我在这篇文章中附上了截图。 How to solve this issue?如何解决这个问题?

.env file .env 文件

CONTENTFUL_ACCESS_TOKEN: process.env.REACT_APP_CONTENTFUL_ACCESS_TOKEN,

config file配置文件

import SETTINGS from "../env";
const contentfulClient = contentful.createClient({
  space: SETTINGS.CONTENTFUL_SPACE,
  accessToken: SETTINGS.CONTENTFUL_ACCESS_TOKEN
});

Do i need to enter api token anywhere?我需要在任何地方输入 api 令牌吗?

.env .env

const SETTINGS = {
  LANDING_BLOG_POSTS: "",
  ICON_TYPE: "svg",
  CONTENTFUL_SPACE: process.env.REACT_APP_CONTENTFUL_SPACE,
  CONTENTFUL_ACCESS_TOKEN: process.env.REACT_APP_CONTENTFUL_ACCESS_TOKEN,
  CLOUDINARY_UNSIGNED_PRESET: process.env.REACT_APP_CLOUDINARY_UNSIGNED_PRESET,
  CLOUDINARY_CLOUD_NAME: process.env.REACT_APP_CLOUDINARY_CLOUD_NAME,
  SERVER_7CHIP: process.env.REACT_APP_SERVER_7HIP || false,
    HARD_CODED_USERNAME: process.env.REACT_APP_TOKEN_USERNAME || "**********",
  HARD_CODED_PASSWORD: process.env.REACT_APP_TOKEN_PASSWORD || "************",
  FACEBOOK_API_ID: process.env.REACT_APP_FACEBOOK_APP_ID,
  FACEBOOK_PAGE_TOKEN: process.env.REACT_APP_FACEBOOK_PAGE_TOKEN,
  FACEBOOK_PAGE_ID: process.env.REACT_APP_FACEBOOK_PAGE_ID || "**************",
  FACEBOOK_SERVER_CALL: true
};
export default  SETTINGS;

Since your code got as far as getting into the createClient function, that tells me your SETTINGS object is loading (otherwise you'd get a TypeError from accessToken: SETTINGS.CONTENTFUL_ACCESS_TOKEN ).由于您的代码已进入createClient function,这告诉我您的 SETTINGS object 正在加载(否则您会从accessToken: SETTINGS.CONTENTFUL_ACCESS_TOKEN获得 TypeError)。 However the error message says that no accessToken was provided.但是错误消息说没有提供accessToken

Therefore SETTINGS.CONTENTFUL_ACCESS_TOKEN is undefined , null or an empty string.因此SETTINGS.CONTENTFUL_ACCESS_TOKENundefinednull或空字符串。 Since that value is set to process.env.REACT_APP_CONTENTFUL_ACCESS_TOKEN this probably means that you have not set your REACT_APP_CONTENTFUL_ACCESS_TOKEN environment variable before running the program.由于该值设置为process.env.REACT_APP_CONTENTFUL_ACCESS_TOKEN这可能意味着您在运行程序之前尚未设置REACT_APP_CONTENTFUL_ACCESS_TOKEN环境变量。

Try setting that environment variable to your access token value, then running the program.尝试将该环境变量设置为您的访问令牌值,然后运行该程序。

If that works, you might want to reconsider the approach.如果可行,您可能需要重新考虑该方法。 You have an "env" file but is getting values from system environment variables.您有一个“env”文件,但正在从系统环境变量中获取值。 Usually you have an "env" file so that you can hardcode values into it, not rely on them being set externally.通常您有一个“env”文件,以便您可以将值硬编码到其中,而不是依赖于外部设置。 If they have to be set externally you might as well not use an "env" file and just use process.env.VARIABLE_NAME directly in your code.如果必须在外部设置它们,您最好不要使用“env”文件,而直接在代码中使用process.env.VARIABLE_NAME

Note that if you hardcode access tokens or other private information in an "env" file, you should add that file to .gitignore so that passwords/keys are not stored in git .请注意,如果您在“env”文件中硬编码访问令牌或其他私人信息,则应将该文件添加到.gitignore中,以便密码/密钥不会存储在git中。

You could use dotenv-webpack module for env variables in react.js.您可以将 dotenv-webpack 模块用于 react.js 中的环境变量。

https://www.npmjs.com/package/dotenv-webpack https://www.npmjs.com/package/dotenv-webpack

And in.env file, env variable should be declared like在.env 文件中,env 变量应该声明为

CONTENTFUL_ACCESS_TOKEN = 'AccessToken'

And call env variable like this in react.js并在 react.js 中像这样调用 env 变量

process.env.CONTENTFUL_ACCESS_TOKEN // should print 'AccessToken'

And then接着

const contentfulClient = contentful.createClient({
  space: process.env.CONTENTFUL_SPACE,
  accessToken: process.env.CONTENTFUL_ACCESS_TOKEN
});

You created.env.development file same location as package.json file.您创建的 .env.development 文件与 package.json 文件的位置相同。

hope, this will help you.希望对你有帮助。

For me, I had the variables in the .env file in the source directory and just had to source it.对我来说,我在源目录中的.env文件中有变量,只需要获取它。 I ran . ./.env我跑了. ./.env . ./.env and that sorted out the error in my case. . ./.env并解决了我的错误。

In my case environment variables weren't accessible, because I created the.env file when my server was already running.在我的情况下,环境变量无法访问,因为我在服务器已经运行时创建了 .env 文件。 I had to restart the server and everything worked like a charm.我不得不重新启动服务器,一切都像一个魅力。

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

相关问题 对 Nuxt.JS “预期参数 accessToken” 感到满意 - Contentful with Nuxt.JS "Expected parameter accessToken" 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)React JS - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) React JS 错误:元素类型无效:预期为字符串React - Error: Element type is invalid: expected a string React React 和 Typescript,出现“预期类型”错误 - React and Typescript, getting "Type expected" error 解析错误:意外的令牌,应为“;” 在反应 JS - Parsing error: Unexpected token, expected “;” in react JS "使用 swiperjs React 时出错 - `请求的模块 'react' 应该是 CommonJS 类型`" - Error using swiperjs React - `The requested module 'react' is expected to be of type CommonJS` React.createElement:类型无效-预期为字符串-index.js - React.createElement: type is invalid — expected a string — index.js 将对象作为参数传递给js函数时出现“预期的”错误 - “Expected ]” error while passing an object as parameter to js function msal typescript 错误属性 'accessToken' 不存在于类型 'void | 令牌响应' - msal typescript error Property 'accessToken' does not exist on type 'void | TokenResponse' React JS'this'不能按预期工作 - React JS 'this' not working as expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM