gpt4 book ai didi

javascript - 类型错误 : gitlab is not a function

转载 作者:行者123 更新时间:2023-11-30 20:16:39 26 4
gpt4 key购买 nike

我的代码 TypeScript 有问题,我正在使用 node gitlab 的库,并且已经正确安装了包,在 @types 中和在 node_modules 中一样多,我正在尝试为 gitlab 做一个 CRUD,而现在我正在用 express 实现后端。当 Controller 执行 gitlab 并编译为 javascript 时,他向我抛出错误: Error throws

这是我的代码:

// 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;

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

最佳答案

您正在从 gitlab 导入所有模块。你可以做

import Gitlab from "gitlab";

然后,

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

因为,您正在执行 import * as gitlab from "gitlab"; 当您执行 new gitlab 时,它不会获取 gitlab 模块

按照你导入的方式,你需要做的是:

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

如需进一步阅读,您可以引用 here

关于javascript - 类型错误 : gitlab is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51860643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com