-1

I'm new in next JS, when I am trying to display my data in a particular route , its not showing , can anyone tell me why its not showing, if you have any query regarding my question please free fell to ask.

Pending Promises

import { getDataFromSheets } from "../../libs/sheets";

export default function handler(req, res) {

    const sheet =  getDataFromSheets();

    console.log(sheet)

    res.status(200).json(sheet);

}
Monu Patil
  • 345
  • 5
  • 18
  • 3
    Your `getDataFromSheets` function is likely asynchronous. Does the part about async/await and promises in this question answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Wing Oct 10 '21 at 11:52
  • Hello @Monu Patil, can you try next time to avoid to post images containing code, instead you can copy directly the output as `code` text. It can improve the quality of the question. Thanks – axel Oct 10 '21 at 11:54
  • Hello @axel, you tagged the wrong person :) – Wing Oct 10 '21 at 11:55
  • ahah, my bad, I fix it now :) – axel Oct 10 '21 at 11:56

2 Answers2

1
import { getDataFromSheets } from "../../libs/sheets";

export default async function handler(req, res) {
  const sheet = await getDataFromSheets();
  console.log(sheet);
  res.status(200).json(sheet);
}
Jacob
  • 1,577
  • 8
  • 24
1

Use can solve this in a couple of ways,

Using async/await

import { getDataFromSheets } from "../../libs/sheets";

export default async function handler(req, res) {
    const sheet =  await getDataFromSheets();
    console.log(sheet)
    res.status(200).json(sheet);
}

Using .then(), .catch()

import { getDataFromSheets } from "../../libs/sheets";

export default function handler(req, res) {
    getDataFromSheets()
      .then(sheet=> res.status(200).json(sheet))
      .catch(err=> console.log(err))
}
Dev-2019
  • 547
  • 3
  • 11