I have a google cloud function which is being executed on a Pub/Sub publish event.
The purpose of the function is to perform updates in various spreadsheets and the sequence of calls to update the spreadsheets is:
- sheets.spreadsheets.values.update (which is used to update a spreadsheet range with values),
- sheets.spreadsheets.get (which is used to retrieve sheet and named range ids) and
- sheets.spreadsheets.batchUpdate (which is used to update a named range)
Initially I created the function in a way that it was updating all spreadsheets in parallel and it was throwing 'read ECONNRESET' (but it was not consistent, there were times I was receiving the error at the start of the function execution, other times after a few calls in values.update or after a few calls in batchUpdate, etc).
I thought that I was doing a lot of requests in parallel and that was causing the issue so the next thing I wanted to try was to change my function to work in a 'synchronous' way, i.e. for each spreadsheet call values.update, get and batchUpdate and then move on to the next spreadsheet, so I used async/await ( and this is the first time actually I am working with async/await so if you see that I am doing something stupid please be gentle :) )
- Node.js version: 8.11.1
googleapis version: 34.0.0
Here is the code I am using which does not work:
const {google} = require('googleapis');
...
const sheets = google.sheets({version: 'v4', auth: createClient(require('xxx.json'))});
...
function createClient (json) {
const client = new google.auth.JWT(
json.client_email,
null,
json.private_key,
['https://www.googleapis.com/auth/drive']
);
client.authorize(function (err, tokens) {
if (err) { console.log(err); }
});
return client;
}
...
const spreadsheetInfo = getSpreadsheetInfo({SpreadsheetId: xxxxx});
...
async function getSpreadsheetInfo (params) {
const request = {
spreadsheetId: params.SpreadsheetId,
ranges: [],
includeGridData: false
};
const response = await sheets.spreadsheets.get(request, {maxContentLength: -1});
return response.data; // This returns undefined
// I have also tried to log the response output to see
// if the spreadsheet object is in a different property but it is not
}
Here is the 'old fashion' code that works as expected:
const {google} = require('googleapis');
...
const sheets = google.sheets({version: 'v4', auth: createClient(require('xxx.json'))});
...
function createClient (json) {
const client = new google.auth.JWT(
json.client_email,
null,
json.private_key,
['https://www.googleapis.com/auth/drive']
);
client.authorize(function (err, tokens) {
if (err) { console.log(err); }
});
return client;
}
...
getSpreadsheetInfo({SpreadsheetId: xxxxx})
.then((response) => {
console.log(response); // This logs spreadsheet object correct
})
.catch((err) => {
console.log(err);
});
...
function getSpreadsheetInfo (params) {
return new Promise((resolve, reject) => {
const request = {
spreadsheetId: params.SpreadsheetId,
ranges: [],
includeGridData: false
};
sheets.spreadsheets.get(request, {maxContentLength: -1}, (err, response) => {
if (err) {
reject(err);
} else {
resolve(response);
}
});
});
}
Can anyone understand what I am doing wrong?
I have a google cloud function which is being executed on a Pub/Sub publish event.
The purpose of the function is to perform updates in various spreadsheets and the sequence of calls to update the spreadsheets is:
Initially I created the function in a way that it was updating all spreadsheets in parallel and it was throwing 'read ECONNRESET' (but it was not consistent, there were times I was receiving the error at the start of the function execution, other times after a few calls in values.update or after a few calls in batchUpdate, etc).
I thought that I was doing a lot of requests in parallel and that was causing the issue so the next thing I wanted to try was to change my function to work in a 'synchronous' way, i.e. for each spreadsheet call values.update, get and batchUpdate and then move on to the next spreadsheet, so I used async/await ( and this is the first time actually I am working with async/await so if you see that I am doing something stupid please be gentle :) )
googleapisversion: 34.0.0Here is the code I am using which does not work:
Here is the 'old fashion' code that works as expected:
Can anyone understand what I am doing wrong?