【问题标题】:Unable to assign types because Postgresql wrapped results in table name无法分配类型,因为 Postgresql 将结果包装在表名中
【发布时间】:2022-01-28 06:49:31
【问题描述】:

我使用 knex 在 PostgreSQL 中创建了一个表:

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

问题表:

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

问题模型:

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

使用 Objection JS 我创建了一个查询来获取问题:

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

然后,我尝试将Question[] 类型分配给结果:

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

错误说我无法分配类型,因为:

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

当我打印输出(不指定类型)时,结果被包装在表名中:

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

为什么结果会包含在表名中?听说是正常的,但其他几乎相同的代码从来没有出现过。

为什么我不能为结果分配类型?

【问题讨论】:

    标签: angular typescript postgresql knex.js objection.js


    【解决方案1】:

    getQuestions 函数返回 Promise 而不是实际的对象。试试const q: Question[] = await getQuestions();

    【讨论】:

    • 这是一个错字。我的错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多