TypeError:> ServerResponse.jsonで未定義のプロパティ 'get'を読み取れませんか?

Nov 20 2020

RDで同様のものを検出し、ユーザーのタイムラインに投稿を広告する単純なクラウド機能を作成しようとすると、次のエラーが発生します。

機能を修正するにはどうすればよいですか?私は何が間違っているのですか?


(以下の2つのエラーは、Firebaseクラウド関数コンソールからのものです)

1.1。

TypeError:ServerResponse.send(/workspace/node_modules/express/lib/response.js:のServerResponse.json(/workspace/node_modules/express/lib/response.js:257:20)で未定義のプロパティ 'get'を読み取れません: 158:21)at likerUIDRef.once.then.catch.error(/workspace/lib/index.js:669:52)at process._tickCallback(internal / process / next_tick.js:68:7)

エラー:プロセスはprocess.on.code(/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:275: 22)object.emit(events.js:198:13)at process.EventEmitter.emit(domain.js:448:20)at process.exit(internal / process / per_thread.js:168:15)atObject。 sendCrashResponse(/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:37:9)at process.on.err(/ layers / google.nodejs.functions-framework / features-framework / node_modules / @ google-cloud / Functions-framework / build / src / invoker.js:271:22)at process.emit(events.js:198:13)at process .EventEmitter.emit(domain.js:448:20)at EmmitPromiseRejectionWarnings(internal / process / promises.js:140:18)at process._tickCallback(internal / process / next_tick.js:69:34)

関連するTypescript:

 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 =>{

(キャッチブロックが実行されます)

then実行されない理由がわかりません。私が得るエラー:

likersユーザー名のフェッチ中にエラーが発生しました:エラー:Reference.updateが失敗しました:最初の引数にプロパティ 'UserFYP.Bke7CYXP31dpyKdBGsiMOEov2q43.0PMdzaOyYBejf1Gh6Pk1RRA5WNJ2.postID.node_.children_.comparator_'のコンテンツが含まれています= function NAME_COMPARATOR(left

回答

2 KevWats Nov 26 2020 at 08:32

なんらかの理由で関数が実際に更新されていないことが問題だと思います。たぶんあなたのlibフォルダが問題です。これを修正するには、Firebase関数ファイルをすべて削除してfirebase initから最初から削除します。

最後に、変数がnullでないことを確認してください。

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

これが問題になるかどうか教えてください。

1 RafaelLemos Nov 25 2020 at 00:23

timelineID参照自体から参照のに移動してみてくださいchild()。次の例のように:

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

これが機能しない場合は、リアルタイムのデータベース構造を共有してください。これにより、達成したいこととデータの形式を簡単に確認できます(答えが正しければ、この行を編集します)。

TahaMalik Nov 22 2020 at 23:51

これらのステートメントを変更する

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

ここで、子メソッドは、fireabaseが格納できる値ではないDocumentSnapshotを返します。

したがって、値を返す唯一のメソッドは val

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