Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
SiguienteJS proceso.env. variables indefinidas al hacer clic en el botón obtener datos

Tengo un problema con las variables ENV. En la primera solicitud con:

 export const getStaticProps = async () => { const posts = await getPosts(1); return { props: posts, revalidate: 5 }; };

todo va bien y obtiene todos los datos, pero al hacer clic en el botón quiero obtener nuevos datos y obtuve 404:

 xhr.js:177 GET http://localhost:3000/fr/undefined/ghost/api/v3/content/posts?key=undefined&fields=id%2Ctitle%2Cfeature_image%2Cslug%2Cexcerpt%2Ccustom_excerpt%2Creading_time%2Ccreated_at&include=authors%2Ctags&page=2 404 (Not Found)

como puede ver, las variables env dan como resultado undefined, no sé por qué. como obtengo los datos:

 const fetchNewData =async (currentPage)=>{ console.log(currentPage); const post = await getPosts(currentPage) console.log(post); }

Cómo uso env

 export const CONTENTKEY=process.env.contentKey export const BLOG_API = process.env.blogApiLink;

 import axios from "axios"; import {BLOG_API,CONTENTKEY} from "../segret_keys" export const getPosts = async (page) => { const pageUrl = BLOG_API + "/ghost/api/v3/content/posts/?key=" + CONTENTKEY + "&fields=id,title,feature_image,slug,excerpt,custom_excerpt,reading_time,created_at&include=authors,tags&page=" + page; console.log(pageUrl); return axios({ method: "get", url: pageUrl, }) //&filter=tag:blog,tag:Blog .then((res) => { return { status: res.status, data: res.data }; }) .catch((err) => { console.log(err.response.status, err.response.data); return { status: err.response.status, data: "" }; }); };
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

De acuerdo con los documentos , puede usar el prefijo NEXT_PUBLIC_ para exponer Environment Variables al navegador:

env.local

 NEXT_PUBLIC_contentKey=somevalue

Usar:

 process.env.NEXT_PUBLIC_contentKey

Otro enfoque:

siguiente.config.js

 module.exports = { publicRuntimeConfig: { contentKey: process.env.contentKey, blogApiLink: process.env.blogApiLink, } }

Acceso al valor de las variables env:

 import getConfig from "next/config"; const { publicRuntimeConfig } = getConfig(); export const CONTENTKEY= publicRuntimeConfig.contentKey export const BLOG_API = publicRuntimeConfig.blogApiLink;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!