If I use callbacks, the code below using Google's Sheets API v4 works fine.
However, I am trying to apply util.promisify
to the API call. This causes:
Cannot read property 'getRoot' of undefined
which is thrown from :
node_modules\googleapis\build\src\apis\sheets\v4.js:592
This line 592 says: context: this.getRoot()
I am probably not using promisify
correctly and I hope that someone here can help me.
I suspect it might have something to do with concurrency.
Any tip would be appreciated.
let {
promisify
} = require('util');
let {
google
} = require('googleapis');
let sheets = google.sheets('v4');
let credentials = require('./credentials.json')
let client = new google.auth.JWT(
credentials.client_email, null, credentials.private_key, ['https://www.googleapis.com/auth/spreadsheets'])
client.authorize((err, tokens) => {
if (err) {
throw err;
}
});
let endpoint = promisify(sheets.spreadsheets.values.get);
async function test() {
let request = {
auth: client,
spreadsheetId: "xxxxxxxx",
range: "'ExampleSheet'!A:B",
valueRenderOption: "UNFORMATTED_VALUE",
majorDimension: "ROWS",
}
let result = await endpoint(request)
.then((res) => {
return res
})
.catch((err) => {
console.log(err);
});
}
test();
That wrapper returns a promise and forwards the call to the original f, tracking the result in the custom callback (**). Here, promisify assumes that the original function expects a callback with exactly two arguments (err, result). That’s what we encounter most often.
The Sheets API gives you full control over the content and appearence of your spreadsheet data.
A call to promisify (f) returns a wrapper around f (*). That wrapper returns a promise and forwards the call to the original f, tracking the result in the custom callback (**). Here, promisify assumes that the original function expects a callback with exactly two arguments (err, result). That’s what we encounter most often.
In Node.js, there’s a built-in util.promisify function for that. Promisification is a great approach, especially when you use async/await (see the next chapter), but not a total replacement for callbacks.
Okay, after some more digging I got it to work.
I modified my original code to use the following:
let endpoint = promisify(api.spreadsheets.get.bind(api));
Not sure why api isn't bound to this
/the context in the first place though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With