简体   繁体   English

firebase 云 function 在回调中获取文档

[英]firebase cloud function get document inside callback

with firebase cloud functions, if I want to reference a document ('/users/' + userId) inside a callback, is this how I would do it?使用 firebase 云函数,如果我想在回调中引用文档('/users/' + userId),我会这样做吗? the userId is inside the first snapshot, so I would need to call another async call to get the user document, but I think something is wrong with my syntax since this gives an error. userId 在第一个快照中,所以我需要调用另一个异步调用来获取用户文档,但我认为我的语法有问题,因为这会出错。

exports.onCommentCreation = functions.firestore.document('/forum/threads/threads/{threadId}/comments/{commentId}')
 .onCreate(async(snapshot, context) => {

     var commentDataSnap = snapshot; 
     var userId = commentDataSnap.data().userId; 
     var userRef = await functions.firestore.document('/users/' + userId).get();
     var userEmail = userRef.data().email; 
});

On this line var userRef = await functions.firestore.document('/users/' + userId).get();在这一行var userRef = await functions.firestore.document('/users/' + userId).get(); change functions.firestore.document to admin.firestore().doc .functions.firestore.document更改为admin.firestore().doc

Something like this:像这样的东西:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const db = admin.firestore();

exports.onCommentCreation = functions.firestore
  .document('/forum/threads/threads/{threadId}/comments/{commentId}')
  .onCreate(async (snapshot, context) => {

    // use const because the values are not changing
    const userId = snapshot.data().userId;
    const userRef = await db.doc('/users/' + userId).get(); // <-- this line
    const userEmail = userRef.data().email;
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM