首页 > 解决方案 > 类型错误:gitlab 不是函数

问题描述

我的代码 TypeScript 有问题,正在使用 node gitlab 库,并且已经正确安装了包,@types 和 node_modules 一样多,我正在尝试为 gitlab 做一个 CRUD,现在我正在完成后端与快递。当控制器执行 gitlab 并编译为 javascript 时,他向我抛出错误: 错误抛出

这是我的代码:

// Import only what we need from express
   import { Router, Request, Response } from 'express';
   import * as gitlab from "gitlab";

// Assign router to the express.Router() instance
   const router: Router = Router();
   const api = new gitlab({
     url: 'http://git.test.com/',
     token: 'asdasfgwsgsafa',
   });

   const users = api.users.all();

   router.get('/allUsers', (req: Request, res: Response) => {
      res.send(users);
   });
 // The / here corresponds to the route that the gitLabController
 // is mounted on in the server.ts file.
 // In this case it's /gitlab
   router.get('/', (req: Request, res: Response) => {
// Reply with a hello world when no name param is provided
     res.send('Hello, World!');
   });

  router.get('/:name', (req: Request, res: Response) => {
    // Extract the name from the request parameters
    res.send(`Hello, ${req.params.name}`);
  });

 // Export the express.Router() instance to be used by server.ts
 export const gitLabController: Router = router;

我希望他们能帮助我,提前谢谢你。

标签: javascriptnode.jstypescriptgitlab

解决方案


您正在从gitlab. 你可以做

import Gitlab from "gitlab";

然后,

const api = new Gitlab({
   url: 'http://git.test.com/',
   token: 'asdasfgwsgsafa'
});

因为,你在做import * as gitlab from "gitlab";的时候不会得到gitlab模块new gitlab

使用您导入的方式,您需要执行以下操作:

const api = new gitlab.Gitlab({
   url: 'http://git.test.com/',
   token: 'asdasfgwsgsafa'
});

如需进一步阅读,您可以参考这里


推荐阅读