简体   繁体   English

在 node.js 中不存在相关性 ID

[英]correlationId dosn't exist in node.js

I am trying to add correlationId using express-correlation-id.我正在尝试使用 express-correlation-id 添加correlationId。 I am exactly following the page: https://www.npmjs.com/package/express-correlation-id .我完全按照页面: https : //www.npmjs.com/package/express-correlation-id I've imported the express-correlation-id pkg and have found it in the package.json and node modules.我已经导入了 express-correlation-id pkg 并在 package.json 和 node 模块中找到了它。

But when i tried to get req.correlationId(), it always said:但是当我尝试获取 req.correlationId() 时,它总是说:

Property 'correlationId' dosn't exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.类型“Request<ParamsDictionary, any, any, ParsedQs>”上不存在属性“correlationId”。

I'm using TypeScript and Express, this is what the code looks like:我正在使用 TypeScript 和 Express,代码如下所示:

import correlator = require("express-correlation-id");
app.use(correlator());
app.get('/', (req, res) => {
    req.correlationId();    // where the error occurs
});

Since this package is written in js language, there are no type definition files for typescript, so you need to extend the Express.Request interface by yourself, add the correlationId method to the interface.由于这个包是用js语言编写的,没有typescript的类型定义文件,所以需要自己扩展Express.Request接口,在接口中添加correlationId方法。

Package versions:包版本:

  • "typescript": "^3.9.7"
  • "express-correlation-id": "^1.3.1",
  • "express": "^4.17.1",

Eg例如

server.ts : server.ts

import express from 'express';
import correlator = require('express-correlation-id');

declare global {
  namespace Express {
    export interface Request {
      correlationId(): string;
    }
  }
}

const app = express();

app.use(correlator());
app.get('/', (req, res) => {
  req.correlationId();
});

This is because the property correlationId doesn't exist in the typing of req .这是因为req的类型中不存在属性correlationId

So, if you see the typing of req is:所以,如果你看到req类型是:

export interface Request<P = ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = ParsedQs> extends http.IncomingMessage, Express.Request {

So, you must create a new Request type with the new functions that you're going to use.因此,您必须使用将要使用的新函数创建一个新的 Request 类型。 For your case:对于您的情况:

import express from "express";
import correlator = require("express-correlation-id");
import { ParamsDictionary, Request } from "express-serve-static-core";
import { ParsedQs } from "qs";

const app = express();

interface CustomReq<P = ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = ParsedQs> extends Request<P, ResBody, ReqBody, ReqQuery> {
    // extended options
    correlationId: () => any;
}

app.use(correlator());
app.get('/', (req: CustomReq, res) => {
    req.correlationId(); // OK
});

As you see in the code above, my new Request is CustomReq and I use the new type for the req .正如您在上面的代码中看到的,我的新RequestCustomReq并且我对req使用了新类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM