作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
环境:
问题:
我无法更新数据库中的用户 token 。我从 typescript 中收到一条错误消息。
typescript 错误信息:
Argument of type '{ token: string; }' is not assignable to parameter of type 'PartialUpdate<User>'. Object literal may only specify known properties, and 'token' does not exist in type 'PartialUpdate<User>'.
问题方法
如果我写@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' },
},
};
}
}
最佳答案
问题是我没有在我的模型中定义变量的类型。官方图书馆的一个例子让我知道我做错了什么 - https://github.com/Vincit/objection.js/tree/master/examples/express-ts
更新模型
export default class User extends Model {
readonly id!: v4;
full_name?: string;
email?: string;
avatar?: string;
provider_data?: {
uid: string;
provider: string;
};
token?: string;
static tableName = 'users';
static jsonSchema = {
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' },
},
};
}
更新方法
const setToken = async (id: any, token: string) => {
try {
User.query()
.update({ token })
.where({ id });
} catch (e) {
throw e;
}
};
关于node.js - Node js + 对象 js + Postgresql。类型 '{ token: string }' 的参数不可分配给类型 'PartialUpdate<User>' 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54705428/
环境: Node js ES6 膝盖:^0.16.3 异议:^1.5.3 pg: ^7.8.0 ~ postgresql 问题: 我无法更新数据库中的用户 token 。我从 typescript 中
我是一名优秀的程序员,十分优秀!