0

I am writing a Typescript nodejs app where I want to iterate over an incoming sql response but I don't know how to fix the syntax error.

const getTicketPriceBrutto = async (eventID: number): Promise<number> => {
  const rows: [] = await query(
    `SELECT ticketPriceCentBrutto FROM events 
    WHERE eventID = '${eventID.toString()}';`
  );
  let price: number;

  //syntax error...
  // return rows.forEach((element: any) => {
  //   price = element.ticketPriceCentBrutto;
  //   return price;
  // });

  //here is also an syntax error
  for (const iterator of rows) {
    price = iterator.ticketPriceCentBrutto;
  }
  return price;
};

How can this be fixed?

2
  • Do you want the sum? Or is the query supposed to return a single row and you want that value? Commented Oct 16, 2022 at 17:36
  • return only a single value. I just use the answers from here and take the 0 entry in the array an return this. Commented Oct 16, 2022 at 19:20

3 Answers 3

1

What is the end result you want to get? An array of prices? If yes then use map:

const prices = rows.map((item: any) => item.ticketPriceCentBrutto)

Also your type for rows is syntatically incorrect, it must be an array of something, if you don't know the exact shape of returned objects then use any[] instead of just [], this could also be a reason for invalid syntax

Sign up to request clarification or add additional context in comments.

Comments

1

In the commented area you have used forEach to iterate rows array. But forEach doesn't return anything. Here is the correct way to use forEach.

const getTicketPriceBrutto = async (eventID: number): Promise<number[]> => {
  const rows: [] = await query(
    `SELECT ticketPriceCentBrutto FROM events 
    WHERE eventID = '${eventID.toString()}';`
  );
  const priceList: number[] = [];

  for (const row of rows) {
    price.push(row.ticketPriceCentBrutto);
  }
  return priceList;

};

Another option is using the map function provided by javascript.

    const getTicketPriceBrutto = async (eventID: number): Promise<number[]> => {
      const rows: [] = await query(
        `SELECT ticketPriceCentBrutto FROM events 
        WHERE eventID = '${eventID.toString()}';`
      ); 
      return rows.map(row => row.ticketPriceCentBrutto);
};

Both ways you can return an array of prices from getTicketPriceBrutto function

Please check forEach and map for more information.

Comments

1

Something like this?

type Row = { ticketPriceCentBrutto: number };

const getTicketPriceBrutto = async (eventID: number): Promise<number | undefined> => {
    const rows: Row[] = await query(
        `SELECT ticketPriceCentBrutto FROM events 
      WHERE eventID = '${eventID}';`
    );

    return rows[0]?.ticketPriceCentBrutto;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.