Property' transformArticles'在类型'类型的文章'上不存在

时间:2017-10-06 18:28:57

标签: javascript typescript

我是打字稿的新手,我不理解我得到的这个错误。我有这样的路线,我正在这样一个名为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");
  }

};

在上面try的第二行,我收到以下错误:Property 'transformArticles' does not exist on type 'typeof Article'.这是什么意思?这就是我的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;
  }

}

1 个答案:

答案 0 :(得分:1)

如果你想打电话:

Articles.transformArticles(...);

您需要将方法设为静态:

export class Article {
    static transformArticles(articles) {

或者,如果您不希望它是静态的,请创建Article

的实例
 const article = new Article();
article.transformArticles(...);