Reputation: 676
I am getting the following errors when trying to make a simple cloud function which detects a like on the RD, and then ads posts to a user timeline.
How can I fix the function? What am I doing wrong?
(2 errors bellow are from the Firebase cloud functions console)
1.
TypeError: Cannot read property 'get' of undefined at ServerResponse.json (/workspace/node_modules/express/lib/response.js:257:20) at ServerResponse.send (/workspace/node_modules/express/lib/response.js:158:21) at likerUIDRef.once.then.catch.error (/workspace/lib/index.js:669:52) at process._tickCallback (internal/process/next_tick.js:68:7)
Error: Process exited with code 16 at process.on.code (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:275:22) at process.emit (events.js:198:13) at process.EventEmitter.emit (domain.js:448:20) at process.exit (internal/process/per_thread.js:168:15) at Object.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/functions-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 emitPromiseRejectionWarnings (internal/process/promises.js:140:18) at process._tickCallback (internal/process/next_tick.js:69:34)
Related 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);
})
Update:
I found something interesting, I get the same 2 errors for other cloud functions as well. And yet those still work. Thus I believe these errors don't matter. The issue still persists however. UserFYP is still not updated.
Update 2:
I have narrowed the problem to a failure at this line:
admin.database().ref(`YourLikes/${uid}`).once("value")
.then(snap =>{
(the catch block runs)
I am unsure why the then
does not run. The error I get:
Error fetching likers username: Error: Reference.update failed: First argument contains a function in property 'UserFYP.Bke7CYXP31dpyKdBGsiMOEov2q43.0PMdzaOyYBejf1Gh6Pk1RRA5WNJ2.postID.node_.children_.comparator_' with contents = function NAME_COMPARATOR(left, right) {
Upvotes: 1
Views: 1782
Reputation: 1296
I believe your problem is that the functions are not actually being updated for some reason. Maybe your lib folder is the problem.
To fix this I would delete all your Firebase function files and then firebase init
from the beginning.
Lastly, make sure that your variables are not null.
let timeStamp = child.child("timeStamp").val();
let newPostID = child.child("postID").val();
let postUid = child.child("uid").val();
Let me know if this verks.
Upvotes: 2
Reputation: 5829
Try moving the timelineID
from the reference itself to the child()
of the reference. Like the following example:
admin.database()
.ref(`UserFYP/${whoLikes}`)
.child(timelineID)
.set({"postID": newPostID, "uid": postUid, "timeStamp": timeStamp})
If this does not work, please share your realtime db structure, as it would make it easier to check what you want to achieve and the format that your data has (I will edit this line out if the answer is correct).
Upvotes: 1
Reputation: 2393
modify these statements
let timeStamp = child.child("timeStamp");
let newPostID = child.child("postID");
let postUid = child.child("uid");
TO:
let timeStamp = child.child("timeStamp").val();
let newPostID = child.child("postID").val();
let postUid = child.child("uid").val();
Explaination:
https://firebase.google.com/docs/reference/node/firebase.database.DataSnapshot#child
here the child mehtod return a DocumentSnapshot which is not a value that fireabase can store.
So, The only Method that return a value is val
https://firebase.google.com/docs/reference/node/firebase.database.DataSnapshot#val
Upvotes: -1