TypeError: Impossibile leggere la proprietà 'get' di undefined in> ServerResponse.json?
Ricevo i seguenti errori quando provo a creare una semplice funzione cloud che rileva un simile su RD e quindi inserisce i post nella sequenza temporale dell'utente.
Come posso riparare la funzione? Che cosa sto facendo di sbagliato?
(2 errori seguenti provengono dalla console delle funzioni cloud di Firebase)
1.
TypeError: Impossibile leggere la proprietà 'get' di undefined su ServerResponse.json (/workspace/node_modules/express/lib/response.js:257:20) su ServerResponse.send (/workspace/node_modules/express/lib/response.js: 158: 21) su likerUIDRef.once.then.catch.error (/workspace/lib/index.js:669:52) su process._tickCallback (internal / process / next_tick.js: 68: 7)
Errore: processo terminato con codice 16 in process.on.code (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:275: 22) su process.emit (events.js: 198: 13) su process.EventEmitter.emit (domain.js: 448: 20) su process.exit (internal / process / per_thread.js: 168: 15) su Object. sendCrashResponse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:37:9) su process.on.err (/ layers / google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js: 271: 22) su process.emit (events.js: 198: 13) al processo .EventEmitter.emit (domain.js: 448: 20) su emitPromiseRejectionWarnings (internal / process / promises.js: 140: 18) su process._tickCallback (internal / process / next_tick.js: 69: 34)
Dattiloscritto correlato:
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);
})
Aggiornare:
Ho trovato qualcosa di interessante, ottengo gli stessi 2 errori anche per altre funzioni cloud. Eppure quelli funzionano ancora. Quindi credo che questi errori non abbiano importanza. Il problema tuttavia persiste. UserFYP non è ancora aggiornato.
Aggiornamento 2:
Ho ristretto il problema a un errore su questa linea:
admin.database().ref(`YourLikes/${uid}`).once("value")
.then(snap =>{
(il blocco di cattura corre)
Non sono sicuro del motivo per cui thennon funziona. L'errore che ottengo:
Errore durante il recupero del nome utente dei likers: Errore: Reference.update non riuscito: il primo argomento contiene una funzione nella proprietà "UserFYP.Bke7CYXP31dpyKdBGsiMOEov2q43.0PMdzaOyYBejf1Gh6Pk1RRA5WNJ2.postID.node_.children_.comparator_", funzione NAMEC a sinistra (a destra)
Risposte
Credo che il tuo problema sia che le funzioni non vengono effettivamente aggiornate per qualche motivo. Forse il problema è la tua cartella lib. Per risolvere questo problema, eliminerei tutti i file della funzione Firebase e poi firebase initdall'inizio.
Infine, assicurati che le tue variabili non siano nulle.
let timeStamp = child.child("timeStamp").val();
let newPostID = child.child("postID").val();
let postUid = child.child("uid").val();
Fammi sapere se questo verks.
Prova a spostare il timelineIDdal riferimento stesso al child()di riferimento. Come il seguente esempio:
admin.database()
.ref(`UserFYP/${whoLikes}`)
.child(timelineID)
.set({"postID": newPostID, "uid": postUid, "timeStamp": timeStamp})
Se questo non funziona, condividi la struttura del tuo database in tempo reale, in quanto renderebbe più facile controllare cosa vuoi ottenere e il formato dei tuoi dati (modificherò questa riga se la risposta è corretta).
modificare queste dichiarazioni
let timeStamp = child.child("timeStamp");
let newPostID = child.child("postID");
let postUid = child.child("uid");
PER:
let timeStamp = child.child("timeStamp").val();
let newPostID = child.child("postID").val();
let postUid = child.child("uid").val();
Spiegazione:
https://firebase.google.com/docs/reference/node/firebase.database.DataSnapshot#child
qui il figlio mehtod restituisce un DocumentSnapshot che non è un valore che fireabase può memorizzare.
Quindi, l'unico metodo che restituisce un valore è val
https://firebase.google.com/docs/reference/node/firebase.database.DataSnapshot#val