为什么我的Env变量显示为未定义?

发布于 2025-02-09 06:02:53 字数 970 浏览 10 评论 0原文

我正在尝试从 supabase 的表中获取数据。当我尝试将URL和键存储在.env文件上时,它会返回不确定。

文件:app.js

import React, { useEffect } from "react";
import ButtonsSection from "./ButtonsSection";
import Header from "./Header";
import InputsSection from "./InputsSection";
import ResultsSection from "./ResultsSection";

export default function App() {
  useEffect(() => {
    console.log(process.env.REACT_APP_SUPABASE_URL);
  }, []);

  return (
    <div>
      <Header />
      <InputsSection />
      <ButtonsSection />
      <ResultsSection />
    </div>
  );
}

文件:database.env

REACT_APP_SUPABASE_URL='https://**********.supabase.co'
REACT_APP_SUPABASE_ANON_KEY='***************.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InF2bW56a2xka25hZG1wcmZtc3JqIiwicm9sZSI6ImFub24iLCJpYXQiOjE2NTU0MDQ4NDQsImV4cCI6MTk3MDk4MDg0NH0.Th_x*******************************'

I'm trying to get data from a table with Supabase. When I try store the URL and the Key on a .env file it comes back as undefined.

File: App.js

import React, { useEffect } from "react";
import ButtonsSection from "./ButtonsSection";
import Header from "./Header";
import InputsSection from "./InputsSection";
import ResultsSection from "./ResultsSection";

export default function App() {
  useEffect(() => {
    console.log(process.env.REACT_APP_SUPABASE_URL);
  }, []);

  return (
    <div>
      <Header />
      <InputsSection />
      <ButtonsSection />
      <ResultsSection />
    </div>
  );
}

File: Database.env

REACT_APP_SUPABASE_URL='https://**********.supabase.co'
REACT_APP_SUPABASE_ANON_KEY='***************.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InF2bW56a2xka25hZG1wcmZtc3JqIiwicm9sZSI6ImFub24iLCJpYXQiOjE2NTU0MDQ4NDQsImV4cCI6MTk3MDk4MDg0NH0.Th_x*******************************'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如此安好 2025-02-16 06:02:53

假设您正在使用Create-React-App。
文件名应该为“ .env”而不是“ database.env”
它应该在项目的根文件夹中。

assuming you are using create-react-app.
File name should be ".env" not "Database.env"
and it should be in the root folder of the project.

风吹雨成花 2025-02-16 06:02:53
  • 步骤1-安装dotenv npm install dotenv
  • 步骤2-创建一个没有名称的文件,但使用.env .env 在root Directory
  • 步骤3 中扩展ES6语法)

问题在您的情况下似乎是您不是在导入dotenv/config,而您的文件名是数据库。ENV,只需将其命名为.env

  • Step 1 - install dotenv npm install dotenv
  • Step 2 - create a file with no name but with .env extension in the root directory
  • Step 3 - import 'dotenv/config' (ES6 Syntax)

the problem in your case seems to be that you are not importing dotenv/config and your file name is database.env which is not correct, just name it as .env

巷子口的你 2025-02-16 06:02:53

我遇到了一个非常相似的问题。我的修复程序是将next_public_添加到.env文件和.env中的createClient()中的变量的开头

NEXT_PUBLIC_REACT_APP_SUPABASE_URL = https://asdf.supabase.co
NEXT_PUBLIC_REACT_APP_SUPABASE_API_KEY = asdfas.asdfasdfsdfasdf

in createClient()中

const supabase = createClient(
  process.env.NEXT_PUBLIC_REACT_APP_SUPABASE_URL,
  process.env.NEXT_PUBLIC_REACT_APP_SUPABASE_API_KEY
);

I was running into a very similar problem getting my Next.js React app to see my Supabase URL and KEY in my .env file. My fix was to add NEXT_PUBLIC_ to the beginning of the variables in the .env file and in CreateClient()

In .env:

NEXT_PUBLIC_REACT_APP_SUPABASE_URL = https://asdf.supabase.co
NEXT_PUBLIC_REACT_APP_SUPABASE_API_KEY = asdfas.asdfasdfsdfasdf

In the CreateClient()

const supabase = createClient(
  process.env.NEXT_PUBLIC_REACT_APP_SUPABASE_URL,
  process.env.NEXT_PUBLIC_REACT_APP_SUPABASE_API_KEY
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文