Property 'count' does not exist on type 'RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader'

272 Views Asked by At

I have the following code:

import { Request, Response, NextFunction } from "express";

import { pool } from "../config/db";

class User {

  static async findAll(req: Request, res: Response, next: NextFunction) {
    await pool.execute("SELECT * FROM users")
      .then((rows) => {
        res.send(rows[0]);
      })
      .catch(err => console.log(err));
    next();

  }

  static async findById(req: Request, res: Response, next: NextFunction) {
    await pool.execute(
      "SELECT * FROM users WHERE id = ?",
      [req.params.id])
      .then(rows => res.status(200).json(rows[0]))
      .catch(err => console.log(err));
    next();

  };


  static async create(req: Request, res: Response, next: NextFunction) {
    await pool.execute(
      "INSERT INTO users (email, password, username , admin) VALUES(?,?,?,?)",
      [req.body.email, req.body.password, req.body.username = req.body.email, req.body.admin])
      .then(() => {
        res.status(200).json(("user created successfully"));
      })
      .catch(err => console.log(err));
    next();


  }


  static async update(req: Request, res: Response, next: NextFunction) {
    await pool.execute(
      "UPDATE users SET email = ?, password = ?, username = ?, admin = ? WHERE id = ?",
      [req.body.email, req.body.password, req.body.username, req.body.admin, req.params.id])
      .then(() => res.status(201).json("updated user successfully!"))
      .catch(err => console.log(err));
    next();

  }


  static async delete(req: Request, res: Response, next: NextFunction) {
    await pool.execute(
      "DELETE FROM users WHERE id = ?",
      [req.params.id])
      .then(() => res.status(201).json(`deleted user ID = ${req.params.id} successfully!`))
      .catch(err => console.log(err));
    next();


  }


  static async count(req: Request, res: Response, next: NextFunction) {
    await pool.execute('SELECT COUNT(*) FROM users;')
      .then(rows => res.status(200).json(parseInt(rows[0].count)))
      .catch(err => console.log(err));
      next();
  }


}


export { User };

I get the following error at this line of the code:

      .then(rows => res.status(200).json(parseInt(rows[0].count)))

Property 'count' does not exist on type 'RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader'.
Property 'count' does not exist on type 'RowDataPacket[]'.ts(2339)

I don't know why this happens and how can I get rid of it?

1

There are 1 best solutions below

5
user1191247 On

Your select query -

SELECT COUNT(*) FROM users;

is returning one row with one column COUNT(*), but your code is trying to access a property (column) called count. If you add a column alias for COUNT(*) as COUNT(*) count it will then be available -

static async count(req: Request, res: Response) {
  pool.execute('SELECT COUNT(*) count FROM users;')
    .then(rows => res.status(200).json(rows[0][0].count))
    .catch(err => console.log(err));
}