【问题标题】:Reference.update failed: First argument contains a function in property?Reference.update 失败:第一个参数包含属性中的函数?
【发布时间】:2021-03-03 11:19:27
【问题描述】:

我在尝试创建一个简单的云功能时遇到以下错误,该功能在 RD 上检测到类似,然后将广告发布到用户时间轴。

如何修复该功能?我做错了什么?


(以下 2 个错误来自 Firebase 云函数控制台)

点赞

获取喜欢的用户名时出错:错误:Reference.update 失败:第一个参数包含属性“UserFYP.Bke7CYXP31dpyKdBGsiMOEov2q43.0PMdzaOyYBejf1Gh6Pk1RRA5WNJ2.postID.node_.children_.comparator_”中的函数,内容 = 函数 NAME_COMPARATOR(左,右){

点赞

TypeError:无法在 ServerResponse.send (/workspace/node_modules/express/lib/response) 处读取 ServerResponse.json (/workspace/node_modules/express/lib/response.js:257:20) 处未定义的属性“get” .js:158:21) 在 likerUIDRef.once.then.catch.error (/workspace/lib/index.js:669:52) 在 process._tickCallback (internal/process/next_tick.js:68:7)

相关打字稿:

 function addPersonalizedFYPPosts(whoLikes: string, postUID: string, postID: string) {
      
      //need to use data to fetch my latest likes
      //then I use the likers data to add the new post to his fypTimeline

      const ref = admin.database().ref(`Likes/${postUID}/${postID}/media1`);
      return ref.once("value") 
      .then(snapshot => {

        //use snapshot to get the my latest like ??
        //Now with this ssnapshot we see other people who liked the same post this liker has. get one of their UIDs and see what else they liked add that to thte likers timeline. 

        var i2 = 0

        snapshot.forEach((theChild) => {

          if (i2 == 0) {

            let uid = theChild.key
          
            //do what you want with the uid
  
            //const userWhoAlsoLiked = snapshot.forEach
  
            const likerUIDRef = admin.database().ref(`YourLikes/${uid}`);
            likerUIDRef.once("value")
            .then(snap =>{
              //const username = snap.val()
              
              var i = 0
              snap.forEach((child) => {
                //UserFYP
                if (i == 0) {
                  let timelineID = child.key;
                  let timeStamp = child.child("timeStamp");
                  let newPostID = child.child("postID");
                  let postUid = child.child("uid");
    
                  //admin.database().ref(`UserFYP/${whoLikes}/${timelineID}/`).update(["":""])
                  admin.database().ref(`UserFYP/${whoLikes}/${timelineID}/`).set({"postID": newPostID, "uid": postUid, "timeStamp": timeStamp})
                  .then(slap =>{
                    console.log("Success updating user FYP: " )
                    return Promise.resolve();
                  })
                  .catch(error => {
                    console.log("Error fetching likers username: " + error)
                    response.status(500).send(error);
                  })
                  i++;
                }
                // return;
              })
              
            })
            .catch(error => {
              console.log("Error fetching likers username: " + error)
              response.status(500).send(error)
            })
            
            return;
            
            i2++;
          }
      })

      })
      .catch(error => {
        console.log("The read failed: " + error)
        response.status(500).send(error)
      })  

    }

export const onPostLike = functions.database
.ref('/Likes/{myUID}/{postID}/media1/{likerUID}')
.onCreate((snapshot, context) => {
  const uid = context.params.likerUID
  const postID = context.params.postID
  const myUID = context.params.myUID
  //addNewFollowToNotif(uid, followerUID)

  return addPersonalizedFYPPosts(uid,myUID,postID);
})

【问题讨论】:

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


    【解决方案1】:

    DataSnapshot 上调用child() 会给您另一个DataSnapshot。所以:

      snap.forEach((child) => {
        //UserFYP
        if (i == 0) {
          let timelineID = child.key;
          let timeStamp = child.child("timeStamp");
          let newPostID = child.child("postID");
          let postUid = child.child("uid");
    

    在此代码中,您的局部变量是快照,其中包含 a.o.访问它们的功能。由于您只能将 JSON 写入数据库,因此快照会被拒绝。

    您可能正在寻找的是:

      snap.forEach((child) => {
        //UserFYP
        if (i == 0) {
          let timelineID = child.key;
          let timeStamp = child.child("timeStamp").val();
          let newPostID = child.child("postID").val();
          let postUid = child.child("uid").val();
    

    【讨论】:

    • 如果我提出的更改消除了您遇到的错误,那么您遇到的是一个新问题。我建议发布一个新问题,因为其他人很有可能比我更了解导致这个新问题的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 2021-02-12
    • 2018-02-22
    • 2016-04-14
    • 2018-08-08
    • 2015-01-31
    • 2018-03-01
    相关资源
    最近更新 更多