0

I'm just transitioning from Google Apps Script to node.js and Google Cloud Functions, and there's a portion of code from an existing post that I don't understand.

In this post: How to use Google sheets API while inside a google cloud function, @domdomegg listed a code sample that calls the Google sheets API from a Google Cloud Function. I've got that code working in a Google Cloud Function.

However, I don't understand a specific segment of the code:

    // This just prints out all Worksheet names as an example
    .then(({ data: { sheets } }) => {
      res.status(200).send({ sheets });

It's the data: { sheets } bit that I don't understand. I'm thinking this is where I need to put my code to write to a Google Sheet, but as it's a bit of a mystery to me. I'm enough of a noob that I can't yet post a comment back to the original post. Any help for a noob?

1 Answer 1

3

That is indeed some sophisticated (tricky) syntax ... lets see if we can't pull it apart.

First is the .then(...). This is what is called when a promise is resolved. The parameter to the then is a function.

In this example, the function is:

({data: {sheets}}) => {
   res.status(200).send({sheets});
}

if that is confusing, it could be re-written as:

function({data: {sheets}}) {
   res.status(200).send({sheets});
}

But what is this oddness of the function parameters? We are used to seeing functions of the form:

function(a, b, c) {
 ...
}

where a, b, and c would be the parameters. In our case the parameters appear to be {data: {sheets}} ... how do we parse that?

The answer appears to be an ECMAScript6 concept called *Destructuring Assignment`. See here:

http://es6-features.org/#ObjectMatchingDeepMatching

it appears that:

function({data: {sheets}}) {
   ...
}

is logically equivalent to:

function(_tmp) {
   let sheets = _tmp.data.sheets;
   ...
}

and finally ...

send({sheets})

is logically:

send({sheets: sheets})

As always, I could be wrong so lets watch for comments and I can either update or delete this answer based on further feedback.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the clarification! So data: {sheets} is an implicit function. I hate those ;-). Additional question: What's data and sheets? I'm assuming those are members of something returned?
@BruceKozuma What is inside the .then(...) is a callback function. It will be called when the previous function is complete, and the argument with which is called will be the result of it. In this case, the argument is an object that contains the key data, which is associated to another object that contains the sheets key. All of this may sound a bit complicated, so I suggest that you replace the function inside the .then(...) for something like : (result) => console.log(result) to see what the arguments passed to this callback function look like.
@BruceKozuma Additionally, I would like to suggest you this link: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… where you can understand a bit more about the feature explained by Kolban. There are also plenty of examples that can be useful to you. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.