gpt4 book ai didi

javascript - 类型 'transformArticles' 上不存在属性 'typeof Article'

转载 作者:行者123 更新时间:2023-12-03 03:20:00 27 4
gpt4 key购买 nike

我是 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 exit 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;
}

}

最佳答案

如果您想调用:

Articles.transformArticles(...);

您需要将该方法设为静态:

export class Article {
static transformArticles(articles) {

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

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

关于javascript - 类型 'transformArticles' 上不存在属性 'typeof Article',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46611863/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com