节点js + Objection js + Postgresql。 “{token:string}”类型的参数不能分配给“PartialUpdate”类型的参数 “
环境:
- 节点js
- ES6
- knex:^ 0.16.3
- 异议:^ 1.5.3
- pg:^ 7.8.0~postgresql
问题:
我无法更新数据库中的用户令牌。我从typescript收到一条错误消息。
打字稿错误信息:
类型'{token:string; }'不能分配给'PartialUpdate <User>'类型的参数。对象文字只能指定已知属性,'PartialUpdate <User>'类型中不存在'token'。
问题方法
如果我写@ts-ignore
,该方法将起作用,但我无法理解。
为什么它会给我一个错误?
import { User } from '@database/models';
...
const setToken = async (id: any, token: string) => {
try {
await transaction(User.knex(), trx =>
User.query(trx)
// An error appears on this line
.update({ token })
.where({ id }),
);
} catch (e) {
throw e;
}
};
我的用户模型
'use strict';
import { Model } from 'objection';
export default class User extends Model {
static get tableName() {
return 'users';
}
static get jsonSchema() {
return {
type: 'object',
properties: {
id: { type: 'uuid' },
full_name: { type: 'string', minLength: 1, maxLength: 255 },
email: { type: 'string', minLength: 1, maxLength: 255 },
avatar: { type: 'string' },
provider_data: {
type: 'object',
properties: {
uid: { type: 'string' },
provider: { type: 'string' },
},
},
token: { type: 'string' },
created_at: { type: 'timestamp' },
updated_at: { type: 'timestamp' },
},
};
}
}
回答如下:
**请提供带有变量partialUpdate的代码。因为您因变量partialUpdate类型的错误声明而出错。 TypeScript完全专注于变量Type,如果变量的类型与您提供给该变量类型的内容不匹配,则会生成错误。您将对象类型值{token:string}传递给您的变量partialUpdate,它只能在您声明它时保存字符串类型变量。 **
PartialUpdate:Object
要么
PartialUpdate = {}
将解决问题。