【问题标题】:how to firebase refernce extract the id in "onCreate" or "onUpdate" triggers?如何在“onCreate”或“onUpdate”触发器中提取id?
【发布时间】:2020-03-24 18:54:42
【问题描述】:

我不想获取 .onUpdate(){id} 下的所有值 var3 是嵌套对象,而 var2 是单个变量。 那么如何从 onUpdate 中检索 {id} 并将其作为参数传递给我可以在 admin.ref() 中使用的 customMethod?

index.ts

export const funcName = functions.region("someRegion").database.ref('/user/{id}/var3').onUpdate((change, context) =>{

      const originalData = <varType> change.after.val();
      const processedData = customMethod(originalData);

      if(change.after.ref.parent)
      return change.after.ref.parent.child('custom').set(processedData);
      else
      return null;
});


function customMethod(data: varType){
 return admin.database().ref('/user/{id}/var2').once('value').then(function(snapshot){
// processing stuff after storing value from var2 in a local variable
}

【问题讨论】:

  • 请添加更多关于您的确切目标和 Firestore 数据库结构的详细信息。如果可以的话,很难理解你的目标。

标签: typescript firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

得到它的工作...引用父节点并将引用作为参数发送。

 const originalData = <varType> change.after.val();
 const splRef = <admin.database.Reference> change.after.ref.parent
 const processedData = customMethod(originalData,splRef);

function customMethod(data: varType,dbref: admin.database.Reference){
 return admin.database().dbref.once('value').then(function(snapshot){
// processing stuff after storing value from var2 in a local variable
}

@Renaud,无论如何谢谢!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-22
    • 2019-05-17
    • 2019-02-11
    • 2018-01-26
    • 2018-07-29
    • 1970-01-01
    • 2020-04-10
    • 2019-02-13
    相关资源
    最近更新 更多