简体   繁体   English

属性“ transformArticles”在类型“ typeof Article”上不存在

[英]Property 'transformArticles' does not exist on type 'typeof Article'

I'm new to typescript and am not understanding this error that I am getting. 我是TypeScript的新手,无法理解我收到的此错误。 I have route where I am pulling in a class called Article like so: 我有一条路线可以进入名为Article的课程,如下所示:

import { Request, Response } from "express";
const appRoot = require("app-root-path");
import { Article } from "./newsArticleModel";
const connection = require(appRoot + "/src/config/connection.ts");
const sql = require("mssql");




async function getNewsData() {
  const pool = await connection;
  const result = await pool.request()
    .input("StoryID", sql.Int, 154147)
    .execute("procedure");
  console.log(result, "the result from the stored procedure");
  return result;
}

sql.on("error", (err) => {
  console.log("the error", err);
});
export let index = async(req: Request, res: Response) => {
  try {
    let articles = await getNewsData();
    articles = Article.transformArticles(articles.recordset);
    articles = JSON.stringify(articles);
    res.render("home", {
      articles,
      title: "Home",
    });
  } catch (e) {
    console.log(e, "teh error");
  }

};

in the second line of the try above, I get the following error: Property 'transformArticles' does not exist on type 'typeof Article'. 在上面的try的第二行中,出现以下错误: Property 'transformArticles' does not exist on type 'typeof Article'. What does this mean? 这是什么意思? This is what my Article class looks like: 这是我的Article类的样子:

const appRoot = require("app-root-path");
import { TransformedRow } from "./transformedRowInterface";

export class Article {
  transformArticles(articles) {
    return articles.map((article) => {
      return this.transformRows(article);
    });
  }

  transformRows(row) {
    const transformedRow: TransformedRow = {
      id: row.StoryID,
      title: row.Title,
      summary: row.Summary,
      body: row.Body,
      synopsis: row.Synopsis,
      author: {
        name: row.AuthorName,
        email: row.AuthorEmail,
      },
      impressions: row.ImpressionCount,
      created: row.CreatedDate,
      updated: row.UpdatedDate,
    };
    return transformedRow;
  }

}

If you want to call: 如果要致电:

Articles.transformArticles(...);

You need to make the method static: 您需要将方法设为静态:

export class Article {
    static transformArticles(articles) {

Or, if you don't want it to be static, create an instance of Article 或者,如果您不希望它是静态的,则创建Article的实例

 const article = new Article();
article.transformArticles(...);

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

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