Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

298
Visualizações
Convert value from promise to string (cookieStore.get question)

I'm attempting to fetch a part of a value from a cookie on a website. So I've written code to get the cookie value, that works fine. And I attempted to do a JSON.stringify() of the returned value into a string, but instead it converts it into an empty object. I suspect this is because the returned object is a Promise object but I don't know why that would make a difference. Here is what I've done so far.

const getCookie = async (name) => {
  const cookie = await cookieStore.get(name);
  if(cookie) {return cookie.value}
  else {console.log('cookie not found')}
}
const cookievalue = getCookie('foo');

And this returns:

Promise {<fulfilled>: 'locale%3Des_us'}

Which is in fact the cookie value that I want. But if I then run:

const newcookievalue = JSON.stringify(cookievalue);

I get:

'[object Promise]'

What I'm trying to do, by the way, is to run an IF statement on that value--if it's "es_us" do one thing, if it's "en_us" do another thing. So for right now the IF statement in place on getCookie is really just testing in the console that I can get and convert the value to a string, before I put the code in my application.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To get a value form an async function you should use await keyword. like this:

 const cookievalue = await getCookie('foo');
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda