javascript - 类型错误: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;
我希望他们能帮助我,提前谢谢你。
解决方案
您正在从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'
});
如需进一步阅读,您可以参考这里
推荐阅读
- linux - Crontab - *、*/1 和 0-59/1 之间的差异
- vb.net - 如何获取在 Windows.Forms PictureBox.Image 中查看的图像的原始大小
- r - 过滤具有多个条件的data.frame而不写出它们
- node.js - GCP - 无法在 Cloud Run 中使用 Google Secret Manager (@google-cloud/secret-manager)
- typescript - TypeScript 中的多态性:宽松类型
- c# - 如何在 c# windows 应用程序中获取颜色的颜色名称字符串
- python - 为什么在 PyPy 中安装模块时出现错误
- makefile - Gnu make:来自subst的宏中的目标
- selenium - 获取 org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:错误
- r - 我可以使用 for 循环在使用 cvxr 的线性问题中指定约束吗?