TypeError: Tidak dapat membaca properti 'get' dari undefined di> ServerResponse.json?
Saya mendapatkan kesalahan berikut saat mencoba membuat fungsi cloud sederhana yang mendeteksi suka di RD, lalu memposting iklan ke timeline pengguna.
Bagaimana cara memperbaiki fungsinya? Apa yang saya lakukan salah?
(2 kesalahan di bawah ini berasal dari konsol fungsi cloud Firebase)
1.
TypeError: Tidak dapat membaca properti 'get' yang tidak ditentukan di ServerResponse.json (/workspace/node_modules/express/lib/response.js:257:20) di ServerResponse.send (/workspace/node_modules/express/lib/response.js: 158: 21) di likerUIDRef.once.then.catch.error (/workspace/lib/index.js:669:52) di process._tickCallback (internal / process / next_tick.js: 68: 7)
Kesalahan: Proses keluar dengan kode 16 di process.on.code (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:275: 22) di process.emit (events.js: 198: 13) di proses.EventEmitter.emit (domain.js: 448: 20) di process.exit (internal / proses / per_thread.js: 168: 15) di Object. sendCrashResponse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:37:9) di process.on.err (/ layers / google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js: 271: 22) di process.emit (events.js: 198: 13) di proses .EventEmitter.emit (domain.js: 448: 20) di emitPromiseRejectionWarnings (internal / process / promise.js: 140: 18) di process._tickCallback (internal / process / next_tick.js: 69: 34)
Skrip Jenis Terkait:
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);
})
Memperbarui:
Saya menemukan sesuatu yang menarik, saya mendapatkan 2 kesalahan yang sama untuk fungsi cloud lainnya juga. Namun itu masih berfungsi. Jadi saya yakin kesalahan ini tidak penting. Masalahnya masih berlanjut. UserFYP masih belum diperbarui.
Perbarui 2:
Saya telah mempersempit masalah menjadi kegagalan di baris ini:
admin.database().ref(`YourLikes/${uid}`).once("value")
.then(snap =>{
(blok tangkap berjalan)
Saya tidak yakin mengapa thentidak berjalan. Kesalahan yang saya dapatkan:
Kesalahan saat mengambil nama pengguna sejenis: Kesalahan: Reference.update gagal: Argumen pertama berisi fungsi dalam properti 'UserFYP.Bke7CYXP31dpyKdBGsiMOEov2q43.0PMdzaOyYBejf1Gh6Pk1RRA5WNJ2.postID.node_.children_.comparator_' dengan konten
Jawaban
Saya yakin masalah Anda adalah bahwa fungsinya tidak benar-benar diperbarui karena beberapa alasan. Mungkin folder lib Anda masalahnya. Untuk memperbaikinya, saya akan menghapus semua file fungsi Firebase Anda dan kemudian firebase initdari awal. </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> orang </s>
Terakhir, pastikan variabel Anda tidak null. </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> </s> orang </s>
let timeStamp = child.child("timeStamp").val();
let newPostID = child.child("postID").val();
let postUid = child.child("uid").val();
Beri tahu saya jika verks ini.
Coba pindahkan timelineIDdari referensi itu sendiri ke child()referensi. Seperti contoh berikut:
admin.database()
.ref(`UserFYP/${whoLikes}`)
.child(timelineID)
.set({"postID": newPostID, "uid": postUid, "timeStamp": timeStamp})
Jika ini tidak berhasil, silakan bagikan struktur db realtime Anda, karena akan lebih mudah untuk memeriksa apa yang ingin Anda capai dan format yang dimiliki data Anda (saya akan mengedit baris ini jika jawabannya benar).
ubah pernyataan ini
let timeStamp = child.child("timeStamp");
let newPostID = child.child("postID");
let postUid = child.child("uid");
UNTUK:
let timeStamp = child.child("timeStamp").val();
let newPostID = child.child("postID").val();
let postUid = child.child("uid").val();
Penjelasan:
https://firebase.google.com/docs/reference/node/firebase.database.DataSnapshot#child
di sini anak mehtod mengembalikan DocumentSnapshot yang bukan merupakan nilai yang dapat disimpan oleh fireabase.
Jadi, Metode satu-satunya yang mengembalikan nilai adalah val
https://firebase.google.com/docs/reference/node/firebase.database.DataSnapshot#val