I am trying to deploy the following function to firebase. The function deploys fine, but when the function triggers I get an error: cannot read property 'parent' of undefined. The error occurs in the first line I reference parent. I used console.log on snapshot and snapshot.ref, and although snapshot exists, snapshot.ref is undefined.
I have used snapshot.ref.parent in other cloud functions and it is working fine. There are two main difference with this function:
(a) it is an onUpdate (I have previously been using onCreate and onDelete)
(b) it is an async function.
exports.likeRating = functions.database.ref('Ocean/{waveId}/Likes').onUpdate(async (snapshot) =>{
let likes; let dislikes; let comments; let echoes;
await snapshot.ref.parent.child('Dislikes').once('value').then(response=>{dislikes = response.val(); return null});
await snapshot.ref.parent.child('Likes').once('value').then(response=>{likes = response.val(); return null});
await snapshot.ref.parent.child('Comments').child('CommentsCount').once('value').then(response=>{comments = response.val(); return null});
await snapshot.ref.parent.child('Echoes').once('value').then(response=>{echoes = response.val(); return null});
snapshot.ref.parent.child('Rating').set(dislikes+likes+comments+echoes);
return null;
}
Any ideas as to why I am getting this error? All help is appreciated.
That function will run significantly slower than it needs to as you're waiting for your requests in series, you should await a Promise.all([<Promises>]) instead, also the return null is redundant.
I'm also not sure why you add everything up each time instead of incrementing the Rating value but maybe I didn't think about it as much as you.
If you look at the docs the signature of the callback is function(non-null functions.Change containing non-null functions.firestore.DocumentSnapshot, optional non-null functions.EventContext)
So the first param is change which contains before and after which are of type DocumentSnapshot, it is those properties you should be using e.g. change.after.ref.
Related
I'm new around here and I'm studying JS! In particular JSON! However, I have come across an exercise that I cannot solve, also because I do not understand what I am doing wrong. I need to extract the information about the planets from the StarWars API. So I do the classic fetch and as a result I get the generic information about the planet in the form of a JSON.
However, I have to extract the planet name and I get stuck, because when I check the PlanetsData variable, it gives me undefined. Ergo the cycle I wrote to extract the names of the planets doesn't work for some reason.
So, my question is:
Why do I get "undefined" for the PlanetsData variable? .. Shouldn't I get the JSON, which displays correctly in the console?
Did I write the cycle correctly?
Thanks to who will answer me!
This is my code:
async function getPlanetsData() {
const planetsData = await fetch ("https://swapi.dev/api/planets").then(data => {
return data.json()}).then(planets => {console.log(planets.results)}) // ---> Here i receive the JSON data
for (let key in planetsData) {
const someInfo = planetsData.results[key].name
console.log(JSON.stringify(someInfo)) } // ---> I don't understand why, but I don't get anything here. There is no response in the console, as if the call did not exist
}
getPlanetsData()
You can write the same function in a different and clearer way,
check the comments to understand the code!
async function getPlanetsData() {
// 1. Fetch and wait for the response
const response = await fetch ("https://swapi.dev/api/planets");
// 2. Handle the response, in that case we return a JSON
// the variable planetsData now have the whole response
const planetsData = await response.json();
// console.log(planetsData); // this will print the whole object
// 3. Return the actual data to the callback
return planetsData;
}
// Function usage
// 4. Call "getPlantesData" function, when it completes we can call ".then()" handler with the "planetsData" that contains your information
getPlanetsData().then(planetsData => {
// 5. Do whatever you want with your JSON object
// in that case I choose to print every planet name
var results = planetsData.results; // result array of the object
results.forEach(result => console.log(result.name));
});
It seems that you have the same issue as : read and save file content into a global variable
Tell us if it does solve your issue or not.
(UPDATE)
To answer explicitly to your questions.
First question:
To get value into variable planetsData you can do this:
async function getPlanetsData() {
const response = await fetch ("https://swapi.dev/api/planets")
const planetsData = await response.json()
for (let key in planetsData) {
const someInfo = planetsData.results[key].name
console.log(JSON.stringify(someInfo))
}
}
getPlanetsData()
Second question:
You didn't write the cycle correctly.
To resolve promises it is preferable to choose between using await and.
here is what I am trying to do with firebase cloud function:
-Listen to any change in one of the documents under 'user' collection.
-Update carbon copies of the userinfo in the relevant documents in both 'comment' and 'post' collections.
Because I will need to query in relevant documents and update them at once, I am writing codes for transaction operations.
Here is the code that I wrote. It returns the error message, 'Function returned undefined, expected Promise or value'.
exports.useInfoUpdate = functions.firestore.document('user/{userid}').onUpdate((change,context) => {
const olduserinfo=change.before.data();
const newuserinfo=change.after.data();
db.runTransaction(t=>{
return t.get(db.collection('comment').where('userinfo','==',olduserinfo))
.then((querysnapshot)=>{
querysnapshot.forEach((doc)=>{
doc.ref.update({userinfo:newuserinfo})
})
})
})
.then(()=>{
db.runTransaction(t=>{
return t.get(db.collection('post').where('userinfo','==',olduserinfo))
.then((querysnapshot)=>{
querysnapshot.forEach((doc)=>{
doc.ref.update({userinfo:newuserinfo})
})
})
})
})
});
I am a bit confused because as far as I know, 'update' method returns a promise? I might be missing something big but I picked up programming only last November, so don't be too harsh. :)
Any advice on how to fix this issue? Thanks!
EDIT:
Building on Renaud's excellent answer, I created the below code in case someone may need it.
The complication with transaction is that the same data may be stored under different indices or in different formats. e.g. The same 'map' variable can be stored under an index in one collection, and as part of an array in another. In this case, each document returned by querying needs different update methods.
I resolved this issue using doc.ref.path, split, and switch methods. This enables application of different update methods based on the collection name. In a nutshell, something like this:
return db.runTransaction(t => {
return t.getAll(...refs)
.then(docs => {
docs.forEach(doc => {
switch (doc.ref.path.split('/')[0]) { //This returns the collection name and switch method assigns a relevant operation to be done.
case 'A':
t = t.update(doc.ref, **do whatever is needed for this collection**)
break;
case 'B':
t = t.update(doc.ref, **do whatever is needed for this collection**)
break;
default:
t = t.update(doc.ref, **do whatever is needed for this collection**)
}
})
})
})
Hope this helps!
Preamble: This is a very interesting use case!!
The problem identified by the error message comes from the fact that you don't return the Promise returned by the runTransaction() method. However there are several other problems in your code.
With the Node.js Server SDK you can indeed pass a query to the transaction's get() method (you cannot with the JavaScript SDK). However, in your case you want to update the documents returned by two queries. You cannot call twice db.runTransaction() because, then, it is not a unique transaction anymore.
So you need to use the getAll() method by passing an unpacked array of DocumentReferences. (Again, note that this getAll() method is only available in the Node.js Server SDK and not in the JavaScript SDK).
The following code will do the trick.
We run the two queries and transform the result in one array of DocumentReferences. Then we call the runTransaction() method and use the spread operator to unpack the array of DocumentReferences and pass it to the getAll() method.
Then we loop over the docs and we chain the calls to the transaction's update() method, since it returns the transaction.
However note that, with this approach, if the results of one of the two original queries change during the transaction, any new or removed documents will not be seen by the transaction.
exports.useInfoUpdate = functions.firestore.document('user/{userid}').onUpdate((change, context) => {
const olduserinfo = change.before.data();
const newuserinfo = change.after.data();
const db = admin.firestore();
const q1 = db.collection('comment').where('userinfo', '==', olduserinfo); // See the remark below: you probably need to use a document field here (e.g. olduserinfo.userinfo)
const q2 = db.collection('post').where('userinfo', '==', olduserinfo);
return Promise.all([q1.get(), q2.get()])
.then(results => {
refs = [];
results.forEach(querySnapshot => {
querySnapshot.forEach(documentSnapshot => {
refs.push(documentSnapshot.ref);
})
});
return db.runTransaction(t => {
return t.getAll(...refs)
.then(docs => {
docs.forEach(doc => {
t = t.update(doc.ref, { userinfo: newuserinfo })
})
})
})
})
});
Two last remarks:
I am not sure that db.collection('comment').where('userinfo', '==', olduserinfo); will be valid as olduserinfo is obtained through change.before.data(). You probably need to specify one field. This is probably the same for newuserinfo.
Note that you cannot do doc.ref.update() in a transaction, you need to call the transaction's update() method, not the one of a DocumentReference.
I'm not sure if this is possible and can't wrap my head around it. I have a larger project where I want to combine a couple of callbacks in different files into one and simply get the data from the different functions. My issue is that I can't seem to fetch the last part which I'll demonstrate in the code.
File one, importing file three and calling the sendUpdate function with 'note' as param.
const three = require("./three")
const note = "test"
three.sendUpdate(note);
File two,
async function getUser(user) {
return await otherfileFarAway.otherFunctionFarAway(user);
}
module.exports = {
getUser
}
File three, where I want to bundle these two up. I'm importing file two
const two = require("two");
async function sendUpdate(note) {
const total = await two.getUser(user);
console.log(note); // Works
console.log(total); // Undefined, duuh
try {
const url
.concat(total)
.concat("/Notes");
const result = await axios.post(
url,
{
Comment: note
},
);
return result.data;
} catch (error) {
logger.axiosError(error);
return null;
}
}
module.exports = {
sendUpdate
}
How would I actually just call getUser in file two from file three and get the value it is getting in file two? Is that possible? If I call it without parameters I get nothing, if I send it with something I get undefined. If I define it beforehand with for example "let user;" I get nothing.
What am I doing wrong or is it just simply not possible to get the returned value?
I suppose you are using Node? If that is that case you need to use exports to make them available via require. Here's some doc
Modify file two to the following to add some debug messages, so you'll see what's wrong:
async function getUser(user) {
console.log(`====== executing getUser() in filetwo with param: `, user);
const result = await otherfileFarAway.otherFunctionFarAway(user);
console.log(`====== returning `, result);
return result;
}
module.exports = {
getUser
}
Your questions seems to be How would I actually just call getUser in file two from file three and get the value it is getting in file two?.
The answer is that you're already calling that filetwo.getUser(...) properly, because otherwise you'd get a syntax error. It's probably that you're not sending the right parameter to it, so otherFunctionFarAway(...) returns nothing/undefined/null.
Edit
After your comment regarding overriding the user var in filetwo, a solution would be, something along the lines:
create and export another function in filetwo which will export the correct user variable
in sendUpdate(...), before calling the present getUser(note) function, make another call to the above function
call getUser() with the result from point 2.
I am building an application customiser in SPFX and I am using pnp/sp to get data from a Sharepoint list - all easy so far. I have figured out the code like this, but it is just returning [object promise] here is my code , any help would be brilliant.
I am calling the function like this :
public emailAddressGetter = this.GetSharePointData();
I am trying to show the output like this :
${escape(this.emailAddressGetter.toString())}
and this is the promise I am executing :
private async GetSharePointData(): Promise<any>
{
let myVar : string;
var resultData: any = await sp.web.lists
.getByTitle('Emails')
.items
.select('EmailAddress')
.getById(99)
.get().then((r => {
myVar = r.EmailAddress;
}));
console.log(myVar);
return myVar;
}
any help would be appreciated, I know I am almost there :) thanks guys
I think your GetSharePointData returns a Promise, because it has async declaration, so you need to execute code asynchronously and wait for the result.
Instead of:
public emailAddressGetter = this.GetSharePointData();
${escape(this.emailAddressGetter.toString())}
Try:
this.GetSharePointData()
.then(res => {
// res here is myVar
${escape(res.toString())};
});
Firstly fix your code's type annotations. You are completely defeating the point of TypeScript by suppressing the errors the language exists to catch by specifying vacuous types instead of leveraging inference. This isn't Java.
async GetSharePointData() { // return type is inferred as `Promise<string>`
const result = await sp.web.lists // the `any` you had here was worse than useless.
.getByTitle('Emails')
.items
.select('EmailAddress')
.getById(99)
.get();
const emailAddress= result.emailAddress;
console.log(emailAddress);
return emailAddress;
}
Now onto async functions and promises. An async function or method always returns a promise. Assigning the result of calling such a function directly to a property or variable will always result in the behavior you described
GetSharePointData().toString() === "[object Promise]"
The correct approach to setting the property emailAddressGetter (BTW that's a terrible name for that property either way) to the email address that the promise eventually resolves with depends on the context, but here is something you might do.
constructor() {
this.emailAddressPromise = this.GetSharePointData();
this.emailAddressPromise.then(emailAddress => this.emailAddress = emailAddress);
}
But that could be awful and unnecessary unpredictable depending on what you are trying to do.
The function worke perfectly, but if I want to print the content of the user, I receive in the firebase log this info:
Function returned undefined, expected Promise or value
The function is:
exports.accountCreate = functions.auth.user().onCreate(user => {
console.log("--->"+user.data);
console.log("ok");
});
Why the user.data is not able to retrieved the informations?
Thanks
Cloud Functions run in a container in a managed environment. The environment tries to minimize how long it keeps the container running, and to be able to do so, it must know when your function is done. Normally in JavaScript code is done when the last } has executed, but this gets more complex when you also need to consider asynchronous operations. For this reason Cloud Functions expects you to inform it when the function is done, in the case of functions.auth.user().onCreate by returning a value or promise.
When you explicitly return a value, it is clear that the function is done. When you explicitly return a promise, it's clear that the function needs to remain active until the promise is resolved/rejected. When you don't return a value, it is not clear what state the function is in.
In your case the fix is simple, and you for example just return true before the final }.
exports.accountCreate = functions.auth.user().onCreate(user => {
console.log("--->"+user.data);
console.log("ok");
return true;
});
The actual value is meaningless btw, a return null would work just as well.