0

I created a table in PostgreSQL using knex:

await knex.schema
    .withSchema('public')
    .createTable('question', (table) => {
        table.increments('id').primary();
        table.text('title').notNullable().index();
        table.text('description').notNullable();
    })

Question Table:

| id | title | description |
| -- | ----- | ----------- |
| 0  | abc   | hello world |
| 1  | def   | Sleep World |
| 2  | ghi   | dream world |

Question model:

export interface Question {
    title: string;
    desciption: string;
}

Using Objection JS I created a query to get the questions:

getQuestions() {
    return Question.query().select('title', 'description');
}

Then, I try to assign Question[] type to the result:

const q: Question[] = await getQuestions();

The error says that I cannot assign the type because:

Type 'Model' is missing the following properties from type 'Question': title, desciption

When I print out the output (without assigning type), the results were wrapped in the table name:

[
  Question { title: 'abc', description: 'hello world' },
  Question { title: 'def', description: 'Sleep World' },
  Question { title: 'ghi', description: 'dream world' },
]

Why is the results wrapped in table name? I heard that they were normal, but it never happened to other nearly identical code.

Why can't I assign types to the results?

ytan11
  • 908
  • 8
  • 18

1 Answers1

0

getQuestions function returns Promise not the actual objects. try const q: Question[] = await getQuestions();

omidh
  • 2,526
  • 2
  • 20
  • 37