I'm encountering an issue with Typescript that I need help understanding. In my code, I have a route where I am importing a class called Article
like this:
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, "the error");
}
};
However, during the execution of the code in the second line of the try
block, I receive the following error:
Property 'transformArticles' does not exist on type 'typeof Article'.
Can someone explain what this error means? Here is how my Article
class is structured:
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;
}
}