【问题标题】:TypeError: Cannot read property 'get' of undefined at > ServerResponse.json?TypeError:无法读取 > ServerResponse.json 处未定义的属性“获取”?
【发布时间】:2021-03-03 13:22:01
【问题描述】:

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

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


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

1.

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

错误:进程以代码 16 退出 在 process.on.code (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:275:22) 在 process.emit (events.js:198:13) 在 process.EventEmitter.emit (domain.js:448:20) 在 process.exit (internal/process/per_thread.js:168:15) 在 Object.sendCrashResponse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:37:9) 在 process.on.err (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:271:22) 在 process.emit (events.js:198:13) 在 process.EventEmitter.emit (domain.js:448:20) 在 emitPromiseRejectionWarnings (internal/process/promises.js:140:18) 在 process._tickCallback (internal/process/next_tick.js:69:34)

相关打字稿:

 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").val();
                  let newPostID = child.child("postID").val();
                  let postUid = child.child("uid").val();
    
                  //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);
})

更新:

我发现了一些有趣的东西,对于其他云功能,我也遇到了同样的 2 个错误。然而那些仍然有效。因此,我相信这些错误无关紧要。然而,问题仍然存在。 UserFYP 仍未更新。

更新 2:

我已将问题缩小到这一行的失败:

        admin.database().ref(`YourLikes/${uid}`).once("value")
        .then(snap =>{

(catch 块运行)

我不确定为什么then 不运行。我得到的错误:

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

【问题讨论】:

  • 该错误表明您的 get 属性有错误,但您的代码中没有任何地方调用此类属性(也没有任何 get() 方法)。
  • 这是完整的代码吗?正如 Renaud 上面提到的,除了 cmets 之外,您的代码中没有对 get 的引用,日志中是否没有更多错误发生?
  • @RafaelLemos 我认为问题出在自动生成的文件上?从错误:(/workspace/lib/index.js:669:52)
  • @RafaelLemos 在调查后也没有任何获取
  • @RenaudTarnec 我更新了帖子

标签: typescript firebase google-cloud-functions


【解决方案1】:

我相信您的问题是由于某种原因实际上并未更新这些功能。也许你的 lib 文件夹是问题所在。 ​​​ 要解决此问题,我将删除您的所有 Firebase 函数文件,然后从头开始删除 firebase init。 ​

最后,确保您的变量不为空。 ​

          let timeStamp = child.child("timeStamp").val();
          let newPostID = child.child("postID").val();
          let postUid = child.child("uid").val();

​ 让我知道这是否可行。

【讨论】:

  • 函数文件夹内的 npm init?
  • 是的,将 NPM 初始化代码放在函数文件夹中...只需在终端上 cd 进入它
【解决方案2】:

尝试将timelineID 从引用本身移动到引用的child()。像下面的例子:

admin.database()
     .ref(`UserFYP/${whoLikes}`)
     .child(timelineID)
     .set({"postID": newPostID, "uid": postUid, "timeStamp": timeStamp})

如果这不起作用,请分享您的实时数据库结构,因为这样可以更轻松地检查您想要实现的目标以及数据的格式(如果答案正确,我将编辑此行)。

【讨论】:

  • 奇怪的是“成功更新用户”在控制台中运行。因此可以假设执行的代码和 userFYP 节点会有一些新数据。但它没有
  • 你能分享你的数据库结构吗?我相信这将有助于形象化预期的结果。
【解决方案3】:

修改这些语句

let timeStamp = child.child("timeStamp");
let newPostID = child.child("postID");
let postUid = child.child("uid");

到:

let timeStamp = child.child("timeStamp").val();
let newPostID = child.child("postID").val();
let postUid = child.child("uid").val();

说明:

https://firebase.google.com/docs/reference/node/firebase.database.DataSnapshot#child

这里的子方法返回一个 DocumentSnapshot,它不是 fireabase 可以存储的值。

所以,返回值的唯一方法是val

https://firebase.google.com/docs/reference/node/firebase.database.DataSnapshot#val

【讨论】:

  • 很抱歉我应该改变它。我已经解决了。问题依然存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-02
相关资源
最近更新 更多