简体   繁体   English

TypeScript 错误 TS2339:类型“用户”上不存在属性“id”

[英]TypeScript error TS2339: Property 'id' does not exist on type ' User'

New to TS and first question here. TS新手和第一个问题在这里。 Please help me understand why I am getting the error below and how I could resolve it:请帮助我理解为什么我会收到以下错误以及如何解决它:

property 'id' does not exist on type 'User' “用户”类型上不存在属性“id”

I have searched extensively on the web and SO to no avail.我在网上广泛搜索,但无济于事。

import { NextFunction, Request, Response, Router } from "express";
import * as HttpStatus from "http-status-codes";
import { body, param, validationResult } from "express-validator/check";

// Import Entities
import { User } from "../entities/user.entity";

// Import Services
import { UserService } from "../services/users.service";

// Import Interfaces
import { IResponseError } from "../resources/interfaces/IResponseError.interface";

/**
 * Returns basic data about the user
 *
 * @Method GET
 * @URL /api/users
 *
 */
usersRouter
  .route("/")

  .get(
    auth.authenticate("jwt"),

    async (req: Request, res: Response, next: NextFunction) => {
      const userService = new UserService();

      try {
        const userId: number = req.user.id;
        const user: User = await userService.getById(userId);

        if (user) {
          res.status(HttpStatus.OK).json({
            name: user.name,
            surname: user.surname,
            email: user.email,
            createdDate: user.createdDate,
            lastSuccessfulLoggedDate: user.lastSuccessfulLoggedDate,
            lastPresentLoggedDate: user.lastPresentLoggedDate,
            lastFailedLoggedDate: user.lastFailedLoggedDate
          });
        }
      } catch (error) {
        const err: IResponseError = {
          success: false,
          code: HttpStatus.BAD_REQUEST,
          error
        };
        next(err);
      }
    }
  );

尝试将 body 添加到 req:

req.body.user.id

暂无
暂无

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

相关问题 Typescript TS2339 属性不存在 - Typescript TS2339 property does not exist 错误TS2339:类型“ {}”上不存在属性“包含” - error TS2339: Property 'includes' does not exist on type '{}' NodeJS + TypeScript 错误 TS2339:“性能”类型上不存在属性“timeOrigin” - NodeJS + TypeScript Error TS2339: Property 'timeOrigin' does not exist on type 'Performance' Express.js/Typescript:错误 TS2339:属性“发送”在类型“响应”上不存在 - Express.js/Typescript: Error TS2339: Property 'send' does not exist on type 'Response' 打字稿错误:TS2339:类型“字符串”上不存在属性“地图” - Typescript Error: TS2339:Property 'map' does not exist on type 'string' 出现“错误 TS2339:“对象”类型上不存在属性“用户”的错误。 ” - Getting error of “error TS2339: Property 'user' does not exist on type 'Object'. ” 将打字稿 ^3.7.2 更新为最新的“打字稿”:“^4.4.4” - 错误 TS2339:“导航器”类型上不存在属性“msSaveOrOpenBlob” - After update typescript ^3.7.2 to latest "typescript": "^4.4.4" - error TS2339: Property 'msSaveOrOpenBlob' does not exist on type 'Navigator' TS2339:类型“导航器”上不存在属性“联系人” - TS2339: Property 'contacts' does not exist on type 'Navigator' TS2339:属性“comparePassword”在类型“Model”上不存在<document, {}> '</document,> - TS2339: Property 'comparePassword' does not exist on type 'Model<Document, {}>' 错误 TS2339:类型 express“应用程序”上不存在属性“使用” - Error TS2339: Property 'use' does not exist on type express "Application"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM