[英]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_TOKEN
是undefined
, null
或空字符串。 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.