octokit / request.js

Send parameterized requests to GitHub’s APIs with sensible defaults in browsers and Node
MIT License
232 stars 60 forks source link

request() method not returning any data when getting user repositories #494

Closed colias-palaeno closed 2 years ago

colias-palaeno commented 2 years ago

Hi,

I'm having an issue with the request() method not returning any data from its promise - it simply returns [object Promise] { ... }, with no other information.

On the README page, it states that if the request promise is resolved, an object with 4 keys will be returned, and if the request promise is rejected, an object with 3 keys will be returned - as you can see, I'm getting neither of these.

Please find my code below:

import { Octokit } from "https://cdn.skypack.dev/@octokit/core";

const octokit = new Octokit({
  auth: 'authkey'
});

const req = octokit.request('GET /users/colias-palaeno/repos', {
  username: 'colias-palaeno',
  sort: 'created'
});

async function returnReqData(){
  await req;
  return req.data;
}

console.log(returnReqData()); // [object Promise] { ... }
gr2m commented 2 years ago

The problem with your code is that you don't await the response

Instead of

console.log(returnReqData());

Try

console.log(await returnReqData());

or

returnReqData().then(console.log)

Here is how you can simplify your code: https://runkit.com/gr2m/octokit-request-js-494/1.0.0

colias-palaeno commented 2 years ago

The problem with your code is that you don't await the response

Instead of

console.log(returnReqData());

Try


console.log(await returnReqData());

I tried both of your alternatives, and the console is simply outputting undefined. Not sure why

gr2m commented 2 years ago

oh sorry I just realized you'll also need to update

async function returnReqData(){
  await req;
  return req.data;
}

to

async function returnReqData(){
  const { data } await req;
  return data;
}

But this code is not very elegant, better look at how I implemented it at https://runkit.com/gr2m/octokit-request-js-494/1.0.0