【问题标题】:Android Cloud Functions get dataAndroid Cloud Functions 获取数据
【发布时间】:2018-01-23 18:51:10
【问题描述】:

我正在使用 Firebase 云函数。我试图在每个帖子上计算喜欢。 我的问题实际上是在 Cloud Functions javascript 代码中。 我正在收听 Firebase 数据库实时中的一个节点,该节点调用“喜欢”。 它看起来像 ->

树是这样保存的:PostID -> UserID -> Data

我想做的是在 Cloud Functions 中获取数据(流派和视频 ID)。

这是代码

   exports.countlikechange = functions.database.ref('/likes/{postid}/{userUID}').onWrite(event => {
const collectionRef = event.data.ref.parent;
const model = event.data.val();

console.log("model",model);

 const countRef = collectionRef.child('likes');


// Return the promise from countRef.transaction() so our function 
// waits for this async event to complete before it exits.
return countRef.transaction(current => {
  if (event.data.exists() && !event.data.previous.exists()) {
    return (current || 0) + 1;
       }
  else if (!event.data.exists() && event.data.previous.exists()) {
    return (current || 0) - 1;
  }
}).then(() => {
  console.log('Counter updated.');
   });
 });

有没有办法从节点获取流派和videoId? 我应该写什么? 例如: const Genre = event.getGenre(类似的东西)

【问题讨论】:

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


    【解决方案1】:

    genrevideoID 位于传递给您的函数的 event.data 快照中。所以:

    const model = event.data.val();
    let genre = model.genre;
    let videoID = model.videoID;
    

    【讨论】:

      猜你喜欢
      • 2018-12-22
      • 2021-12-02
      • 1970-01-01
      • 2020-10-17
      • 2021-07-25
      • 2019-10-17
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多