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

159
Views
¿Cómo definir y pasar por separado un nombre de colección a una función de nube de Firebase?

Tengo la siguiente función en la nube que crea un documento en una colección 'student_history' para cada creación de un nuevo documento en la colección 'students':

 document("students/{student_id}").onCreate( async (snap, context) => { const values = snap.data(); console.log(values); console.log(typeof values); return db.collection("student_history").add({...values, createdAt:FieldValue.serverTimestamp()}); });

Quería generalizar esto para otras 2 colecciones. Algo como esto:

 export const onStudentCreated = functions.firestore.document('/students/{id}').onCreate(onDocCreated); export const onBatchCreated = functions.firestore.document('/batches/{id}').onCreate(onDocCreated); export const onTeacherCreated = functions.firestore.document('/teachers/{id}').onCreate(onDocCreated);

Mi pregunta es, ¿cómo puedo hacer que mi función onDocCreated reciba un nombre de colección (p. ej., estudiantes, lotes o profesores) y haga una entrada en el historial de estudiantes, el historial de lotes o el historial de profesores correspondiente?

 async function onDocCreated() { async (snap, context) => { const values = snap.data(); console.log(values); console.log(typeof values); return db.collection("NAMEOFTHECOLLECTION_history").add({ ...values, createdAt: FieldValue.serverTimestamp() }); } }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

En primer lugar, deberá pasar los parámetros snap y context en la propia función onDocCreated() . El snap es un QueryDocumentSnapshot , por lo que puede obtener el ID de la colección de la propiedad parent , como se muestra a continuación:

 async function onDocCreated(snap, context) { const values = snap.data(); console.log(values); const collectionName = snap.ref.parent.id; console.log("Collection Name:", collectionName) return db.collection(`${collectionName}_history`).add({ ...values, createdAt: admin.firestore.FieldValue.serverTimestamp(), }); }
about 4 years ago · Juan Pablo Isaza Report

0

Agregando a la respuesta de @Dharamaj,

Según su sugerencia

 export const onStudentCreated = functions.firestore.document('/students/{id}').onCreate(onDocCreated); export const onBatchCreated = functions.firestore.document('/batches/{id}').onCreate(onDocCreated); export const onTeacherCreated = functions.firestore.document('/teachers/{id}').onCreate(onDocCreated);

necesita ser reemplazado con:

 exports.onStudentCreated = functions.firestore.document('/students/{id}').onCreate(onDocCreated); exports.onBatchCreated = functions.firestore.document('/batches/{id}').onCreate(onDocCreated); exports.onTeacherCreated = functions.firestore.document('/teachers/{id}').onCreate(onDocCreated);
about 4 years ago · Juan Pablo Isaza 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!