Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
colias-palaeno opened this issue Jul 24, 2022 · 3 comments
Closed
Labels
Type: Support Any questions, information, or general needs around the SDK or GitHub APIs
Projects

Comments

@colias-palaeno
Copy link

colias-palaeno commented Jul 24, 2022

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] { ... }
@ghost ghost added this to Inbox in JS Jul 24, 2022
@wolfy1339 wolfy1339 added the Type: Support Any questions, information, or general needs around the SDK or GitHub APIs label Jul 24, 2022
@ghost ghost moved this from Inbox to Support in JS Jul 24, 2022
@gr2m
Copy link
Contributor

gr2m commented Jul 26, 2022

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

@gr2m gr2m closed this as completed Jul 26, 2022
JS automation moved this from Support to Done Jul 26, 2022
@colias-palaeno
Copy link
Author

colias-palaeno commented Jul 27, 2022

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
Copy link
Contributor

gr2m commented Jul 27, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Support Any questions, information, or general needs around the SDK or GitHub APIs
Projects
No open projects
JS
  
Done
Development

No branches or pull requests

3 participants