Skip to content
Advertisement

How do I map recordset to an array in React?

I am trying to return an array. I read a recordset from SQL and get data:

+------------+------------+------------+
| start_type | field_name | start_text |
+------------+------------+------------+
| 0          | Field1     | some text. |
+------------+------------+------------+
| 0          | Field2     | some more  |
+------------+------------+------------+
| 0          | Field3     | still more |
+------------+------------+------------+

I want to return this as an array, I try (in a function with Promise<StartText>):

var results: Array<StartText>=[];
results=result.recordset;
return results;

with StartText:

    export class StartText {
  constructor(
    public startTextId: number,
    public fieldName: string,
    public startText: string
  ) {}

  static from(row: {
    start_text_id: number;
    field_name: string;
    start_text: string;
  }): StartText {
    return new StartText(
      row.start_text_id,
      row.field_name,
      row.start_text
    );
  }

  static toList(result: { recordset: any[] }): StartText[] {
    const StartTexts: StartText[] = [];
    result.recordset.forEach((element: any) => {
      StartTexts.push(StartText.from(element));
    });
    return StartTexts;
  }
}

but this says:

'StartText[]' is missing the following properties from type 'StartText': startTextId, fieldName, startText.

So how do I return the recordset (actually ideally I would like to return a single value for start_type and a dictionary of field_name: start_text pairs but first things first).

Advertisement

Answer

I changed the promise to: Promise <Array<StartText>>.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement