2021年1月5日星期二

Manipulation of fetch data from spreadsheet returns promise, not object

I am fetching and processing the data from a spreadsheet as follows:

const getJsonSheet = async () => {      const response = await fetch('<FETCH-SPECIFIC-SPREADSHEET-LINK>');      const data = await response.json();      let cells = await data.feed.entry;      let table = {          massa:[],          description:[],          price:[],          imgLink:[]      };      await cells.map( cell => {          if ( parseInt(cell.gs$cell.row) > 1) {              let rowValue = parseInt(cell.gs$cell.row) - 2;              let inputValue = cell.gs$cell.inputValue;                switch (cell.gs$cell.col) {                  case "1":                      table.massa[rowValue] = inputValue;                      break;                  case "2":                      table.description[rowValue] = inputValue;                      break;                  case "3":                      table.price[rowValue] = inputValue;                      break;                  case "4":                      table.imgLink[rowValue] = inputValue;                      break;                  default:                      console.log('Cell out of range.')                  }          }              })        return table;  };    export default getJsonSheet;  

I import and execute getJsonSheet() in another file, assigning it's value to a variable, then I console.log this variable. I expected to have the table object logged into the console, but actually a Promise is logged. However, this logged promise is fullfilled and has the "table" object as it's result. I don't yet fully understand Promises, async and await - I'm new to this specific topic.

Is there a way to get the "table" object directly assigned to a variable so I can manipulate the DOM somewhere else using it?

PS:the data is being fetched correctly, since all I wanted is in the promise's result. My problem is in getting the promise's result assigned to a variable.

https://stackoverflow.com/questions/65589161/manipulation-of-fetch-data-from-spreadsheet-returns-promise-not-object January 06, 2021 at 09:56AM

没有评论:

发表评论