Javascript 如何管理Objective.js中的验证与原始值
我在objective.js中有这个模型:Javascript 如何管理Objective.js中的验证与原始值,javascript,node.js,knex.js,objection.js,Javascript,Node.js,Knex.js,Objection.js,我在objective.js中有这个模型: class UserAccess extends Model { static get tableName() { return 'user_access' } static get jsonSchema() { return { type: 'object', properties: { id: {
class UserAccess extends Model {
static get tableName() {
return 'user_access'
}
static get jsonSchema() {
return {
type: 'object',
properties: {
id: {
type: 'integer'
},
user_id: {
type: 'integer'
}
timestamp: {
type: 'string',
format: 'date-time'
},
},
additionalProperties: false
}
}
$beforeInsert() {
this.timestamp = this.$knex().raw('now()')
}
static async insert(data) {
const result = await this.query().insert(data)
return result.id
}
我需要在时间戳
列中插入数据库时间。当Objective执行验证时,timestamp
的值是原始Knex函数的一个值:
Raw {
client:
Client_MySQL2 {
config: { client: 'mysql2', connection: [Object] },
connectionSettings:
{ user: '',
password: '',
host: '',
database: '' },
driver:
{ createConnection: [Function],
connect: [Function],
// continues
因此,在执行验证时,会返回一个错误,因为它不是字符串:
Validation error: "timestamp" should be a string
是否有任何方法可以使用数据库时间和保持验证?您必须使用“.then()”链接查询生成器以获取查询结果或使用async/Wait。这可能会奏效:
async $beforeInsert() {
this.timestamp = await this.$knex().raw('now()')
}
或:
谢谢!但是我不明白为什么
knex().raw()
不返回未解决的承诺,而在我的示例中,无论如何,beforeist
在验证后执行。我刚刚意识到我的例子不适合我的问题。。。
$beforeInsert() {
this.timestamp = this.$knex().raw('now()').then(function(result) {
console.log(result)
})
}