The function 'transformArticles' is not recognized as a property of the object 'Article'

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;
  }

}

Answer №1

If you feel like reaching out:

Articles.morphArticles(...);

To accomplish this, the method must be declared as static:

export class Article {
    static morphArticles(articles) {

Alternatively, if a static approach doesn't suit your needs, instantiate an Article object

 const blogPost = new Article();
blogPost.morphArticles(...);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to animate share buttons using Framer Motion but staggering siblings?

I have a Share Icon that looks like: I'm looking to display the first 5 icons, with the 5th icon being the share icon. The remaining icons should appear below the share icon and expand to the right when someone taps or hovers over it (either tap or h ...

Numerous JSON entities

Curious to know if it's achievable to load more than one JSON file with just a single jQuery.ajax() call. Or do I have to make separate calls for each file? Warm regards, Smccullough ...

What is the best way to transform a standard array into a record without losing the specific data types in each position?

Imagine type Individual = { name: string; age: number; }; const john = { name: "John", age: 28, } as const; const emily = { name: "Emily", age: 35, } as const; I am looking to create a function that takes an individual an ...

AngularJS: Display the last four characters of a string and substitute the rest with 'X'

I am attempting to change the characters with X and make it look something like this XXXXXT123 This is what I have tried: var sno = 'TEST123'; alert(sno.slice(0,3).replaceWith('X')); However, I encountered an error in the console ...

Having trouble getting my Sequelize model to export properly

For my school project, I am developing an e-commerce website and encountering an issue with connecting my GoogleStrategy to my SQLite database. The goal is to store user data in a table, but whenever I try to call the variable in my passport-setup file, an ...

Guide on accessing POST data in jQuery

Similar Question: how to retrieve GET and POST variables using JQuery? This is the HTML snippet I am working with: <form action='.' method='post'>{% csrf_token %} <div class="parameters"> Show & ...

Struggling to use the bind method for the loadScene callback function in cocosCreator or cocos2d-x?

When the loadScene() callback function is bound, the information retrieved from getScene() becomes inaccurate. Upon transitioning from the Entry Scene to the Lobby Scene, I perform post-processing tasks. The implementation was done in TypeScript. Entry. ...

Can 2D canvas elements make use of CSS shaders?

Can CSS shaders, like "fragment" shaders, be utilized in 2D canvas contexts as well? ...

Angular is notifying that an unused expression was found where it was expecting an assignment or function call

Currently, I am working on creating a registration form in Angular. My goal is to verify if the User's username exists and then assign that value to the object if it is not null. loadData(data: User) { data.username && (this.registrationD ...

Creating a mandatory and meaningful text input in Angular 2 is essential for a

I am trying to ensure that a text material input in my app is mandatory, with a message like "Please enter issue description." However, I have noticed that users can bypass this by entering spaces or meaningless characters like "xxx." Is there an npm pac ...

Populating a data grid with several objects within a JSON object

I am currently developing a project utilizing React with typescript and materialUi. My task is to retrieve data from a JSON fetch request and display it in a DataGrid. The structure of the JSON data is as follows: { id: "1234567890", number: ...

NPM packages installed on a global level are not being detected

Recently, I experienced a system crash that led me to delete some files in an attempt to fix the issue. Among those files may have been ~/.profile. Since restoring my system, I've noticed that my globally installed npm packages are no longer functioni ...

Modifying the CSS based on the SQL data retrieved

I'm currently diving into the world of php and jquery, attempting to build a webpage that displays player information and their status fetched from my Mysql server. Although the core code is functional, it's a mashup of snippets gathered from va ...

Receiving no communication from Express Router

Having trouble receiving a response from the server after making get/post requests. I've tried adjusting the order of functions in index.js without success. I also attempted to send a post request using Postman to localhost:8080/register, but the requ ...

Using React to display data from a nested JSON object in a table

I am currently working on parsing a JSON object into a table using React. However, I am facing an issue with utilizing the .map() function to create a row for every unique combination of course code, name, transferable_credits, transferable_credits -> i ...

Retrieving Records within a Specific Date Range for Check-ins and Check-outs

Our team is currently working on developing booking software that requires handling different room pricing based on specific dates. Each room has distinct pricing for weekdays and weekends. ROOM 1 Pricing 1: September 1st - November 3rd, Weekday: $100, W ...

Enhance your Next JS website's SEO with a combination of static pages, SSR pages, and client-side

In my project using Apollo GraphQL with Next JS, I have explored three different approaches to querying and rendering data. The first method involves Static Rendering by utilizing getStaticProps(), which looks like the following: export async function getS ...

Searching for a different method in JavaScript that can add items without duplication, as the prependTo function tends to insert multiple items

Every time my code runs successfully, a success message is generated and displayed using prependTo within the HTML. However, the issue arises when the user performs the successful action twice, resulting in two success messages being shown on the screen. ...

Can the hexadecimal value from an input type color be extracted and used to populate form fields that will then be displayed in a table after submission?

Hello everyone, I'm new to this platform and seeking guidance on how to improve my post! I recently created a CRUD app using Angular. It consists of a basic form with 4 fields, a color picker using input type='color', and a submit button. U ...

How to use OBJLoader/Three.js to load a specific URL in JavaScript

I am encountering an issue with a JavaScript code snippet from the three.js OBJloader. I should mention that my experience level in JS and PHP is limited as I am just starting out with WordPress websites. My problem lies in utilizing the setPath and load ...