monkeyUser
monkeyUser

Reputation: 4671

Get credential in nodejs google sheet v4

I'm following this tutorial https://developers.google.com/sheets/api/quickstart/nodejs and I'm able to read and update my sheet.

I'd like to get the credential in order to pass more parameters to my function.

authorize(JSON.parse(content), listMajors);

How can I pass 1 more parameter to listMajor? Or How can I get auth to pass my own function like listMajors?

function listMajors(auth) {

Upvotes: 0

Views: 67

Answers (1)

Tanaike
Tanaike

Reputation: 201378

In your case, in order to add one more parameter to the arguments in listMajors, how about using call and/or apply?

Using call:

authorize(JSON.parse(content), (auth) => listMajors.call(this, auth, "additionalParameter"));  // or listMajors.call(null, auth, "additionalParameter")

Using apply:

authorize(JSON.parse(content), (auth) => listMajors.apply(this, [auth, "additionalParameter"]));  // or listMajors.apply(null, [auth, "additionalParameter"])

Note:

  • In above modification, the arguments can be used like function listMajors(auth, parameter){}.

References:

Added:

I add 2 other methods.

1. Using function:

authorize(JSON.parse(content), (auth) => listMajors(auth, "additionalParameter"));

In this case, the arguments can be used like function listMajors(auth, parameter){}.

2. Using bind:

authorize(JSON.parse(content), listMajors.bind(this, "additionalParameter"));  // or listMajors.bind(null, "additionalParameter")

In this case, the arguments can be used like function listMajors(parameter, auth){}.

Reference:

Upvotes: 1

Related Questions