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

184
Visualizações
Firebase REST get data

After watching Firebase Cloud Function Tutorial - REST API Part 1 | Diligent Dev, I have spent several hours trying to understand how to get the entire record that was posted into firestore. Here is index.js code:

const functions = require("firebase-functions");
const express = require("express");
const cors = require("cors");
const admin = require("firebase-admin");
admin.initializeApp();
const app = express();
app.use(cors({origin: true}));


app.post("/", async (req, res) => {
    const user = req.body;
    await admin.firestore().collection("users").add(user);
    res.status(201).send(JSON.stringify(user));
  }); 

The JSON data I posted was:

{"id":123, "name":"Tony Stark", "email":"ironMan@MarvelComics.com"}

In this get, the id is returned but I can't seem to access the other properties that are part of the record (example name: Tony Stark)?

  app.get("/:id", async (req, res) => {
    const snapshot = await
    admin.firestore().collection("users").doc(req.params.id).get();
    const userId = snapshot.id;
    const userData = snapshot.data():
    constole.log(userData);
    res.status(200).send(JSON.stringify({id: userId, ...userData}));
  });

There must be a simple solution but console.log(userData) is not the answer! :-(

Almost forgot to say, my request from the browser is passing the user id as a query parameter, in this example localhost:5001/projectReference/user/1 and I also tried with the Cloud Firestore document id (.../user/XRAwtfj9FSubeCArvUtQ). In each case I see in the browser the JSON string {"id":"the_value_requested"} but no userData.

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

0

What does console.dir(userData) output? That should help you see what's in the returned object.

Are you confident that name and email are avtually present on the record in Firebase? It's possible your retrieval is working as expected but the data expected isn't there.

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