1

I have this function

  function getAvailableProviders(tmpDir, snips, processRepo) {
  const octokit = require("@octokit/rest");
  octokit.repos.listForOrg(
    { org: "terraform-providers", type: "public", per_page: 100 },
    (error, result) => {
      result.data.forEach((element) => {
        processRepo(tmpDir, element.name, snips, iterateOnDocFiles);
      });
    },
  );
}

When I run my program, I get this error from the typescript compiler

(node:15713) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'listForOrg' of undefined

The problem is according to the octokit documentation, the repos.listForOrg is defined.

https://octokit.github.io/rest.js/v18#repos-list-for-org

I'm just trying to learn (typescript and node), while at the same time recreate something that has been abandoned by the original devleoper. I have my require statement, according to the octokit documentation. it's not reaching out to the api. Is there something else I need to have installed in my node project to make this work correctly?

Chief
  • 130
  • 10
  • Have you looked at the usage documentation: https://octokit.github.io/rest.js/v18#usage? It looks like you need to destructure `Octokit`: `const { Octokit } = require("@octokit/rest");`. And then set it up with the proper configuration and the return value of that is what exposes `repos.listForOrg`. – skovy Nov 04 '20 at 00:40
  • isn't that what I'm doing with line 2 ?? – Chief Nov 04 '20 at 00:47
  • for what it's worth, I rewrote that line same as in the docs, and listed above, same error. – Chief Nov 04 '20 at 00:52

1 Answers1

0

I think you need a slight change to your require and add const octokit = new Octokit(); after the require (insert as line 3 in your example):

  function getAvailableProviders(tmpDir, snips, processRepo) {
  const { Octokit } = require('@octokit/rest'); // <-- change this line
  const octokit = new Octokit(); // <-- add this line
  octokit.repos.listForOrg(
    { org: "terraform-providers", type: "public", per_page: 100 },
    (error, result) => {
      result.data.forEach((element) => {
        processRepo(tmpDir, element.name, snips, iterateOnDocFiles);
      });
    },
  );
}

see octokit readme

David Burson
  • 2,947
  • 7
  • 32
  • 55