0

I'm learning NodeJS a bit to do some automation scripts, but I'm having a problem that I don't know how to solve.

Using google spreadsheet API and "promisify-node" package, I'm requesting some data from an spreadsheet, and trying to return part of the data to be used on another js file.

file: spreadsheet.js

const getProductsConfiguration = async(auth) => 
{
    const sheets = google.sheets('v4');
    const getValues = promisify(sheets.spreadsheets.values.get);
    await getValues({
        auth: auth,
        spreadsheetId: utils.getSpreadsheedId(config.spreadsheet.configSpreadsheedUrl),
        range: `${config.spreadsheet.configTabName}!A8:C1000`,   
    })
    .then(function (response) {
        var productsConfiguration = [];
        for(var value in response.values)
        {
            if(response.values[value].length === 0) continue;
            var productConfig = {
                "productName": response.values[value][0],
                "spreadsheetId": response.values[value][1],
                "filterId": response.values[value][2]
            };
            productsConfiguration.push(productConfig);
        }
        console.log(productsConfiguration);
        return productsConfiguration;
    })
    .catch(function (error){console.log(error); return false;});
};

app.js:

productsConfig = await spreadsheet.getProductsConfiguration(sheetAuth);
        console.log(productsConfig);

this console.log is returning "undefined", but the console.log inside "spreadsheet.js" is returning the correct data.

What can I do to solve? thanks

  • 1
    try `const values = await getValues({ ...` and `return values` at the end of `getProductsConfiguration` – Taki Mar 08 '19 at 17:09
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Serge K. Mar 08 '19 at 17:10

1 Answers1

0

You can just do const values = await getValues({...}).No need to put a then after it since the promise resolves to the values when you use await in front of a function that returns a promise.

const getProductsConfiguration = async(auth) => {
    const sheets = google.sheets('v4');
    const getValues = promisify(sheets.spreadsheets.values.get);
    let values
    try{
        values = await getValues({
            auth: auth,
            spreadsheetId: utils.getSpreadsheedId(config.spreadsheet.configSpreadsheedUrl),
            range: `${config.spreadsheet.configTabName}!A8:C1000`,   
        })
    }catch(e){
        console.log(error)
    }

    var productsConfiguration = [];
    if(values){
        for(var value in values) {
            if(values[value].length === 0) continue;
            var productConfig = {
                "productName": values[value][0],
                "spreadsheetId": values[value][1],
                "filterId": values[value][2]
            };
            productsConfiguration.push(productConfig);
        }
    }

    console.log(productsConfiguration);
    return productsConfiguration
};

Hope this helps !

Hemant Parashar
  • 3,684
  • 2
  • 16
  • 23
  • Ok, I did more or less what you said, and it worked :D thank you.. So, just one more question. Everytime I want to return a value inside of a Promise, I need to store the whole promise in a variable, and then return this variable? Thank you! – LucasGrizante Mar 08 '19 at 17:25
  • A promise returns a promise. Even if you return a value from a promise, its returned wrapped in a promise...thats why your'e able to call then on it. But using `await` in front of a promise ,inside an `async function`, resolves that promise into the value. – Hemant Parashar Mar 08 '19 at 17:34