简体   繁体   中英

How to pass value getting from Google sheets to Node JS to show into React Native as a Flatlist

There is an async/await function which gets data from the Google sheets. I have used a return statement for a single cell value in the end of the function and when I pass this into node JS then it throws an error that argument must be of type string or array not a function. I am unable to pass the values from this function to React native app. Below is the sample code

    async function gsrun(cl){
    const gsapi = google.sheets({version : "v4", auth: cl});
    const opt = {
        spreadsheetId : '<spreadsheet id>',
        range: 'Range to get values'
    }
    var data = await gsapi.spreadsheets.values.get(opt);
    var dataArray = data.data.values;            
    return dataArray[2][1]
    }
    var a = gsrun(client)
    app.get("/", function(req, res){
    res.send(a);
    })

Issues/Solution:

  • You're passing a function to res.send . You should call the function with cl first.
  • async function returns a promise. You need to await it's resolution

Snippet:

app.get("/", async function(req, res){ res.send(await gsrun(client));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM