javascript - AdonisJs - 如何将我的日期字段与数据库中的日期相等
问题描述
我创建了一个仅包含日期输入的视图,以通过 created_at 字段查询寄存器。我的日期输入搜索 ?date=2020-07-10,而我的数据库保存为 Data: Fri Jul 10 2020 08:41:12 GMT-0300 (GMT-03:00)。这样我的搜索总是失败并且什么也没找到
如何将我的日期字段与数据库中的日期相等?
我的观点
<form action="/buscarpordata" method="GET">
<h1 class="title is-4 column is-4 is-offset-4">Buscar Nota por Data</h1>
<div class="field is-grouped column is-4 is-offset-4">
<input type="date" name="data" required>
<br><div style="margin-bottom: 20px;">
<p class="control">
<input type="submit" class="button is-primary is-medium is-6" value="Buscar">
</p>
</div>
</div>
</form>
我的迁移
'use strict'
/** @type {import('@adonisjs/lucid/src/Schema')} */
const Schema = use('Schema')
class NotaSchema extends Schema {
up () {
this.create('notas', (table) => {
table.increments()
table.string('numero')
//FORNECEDOR FK
table.integer('id_fornecedor_fk')
.unsigned()
.references('id')
.inTable('fornecedors')
.onUpdate('CASCADE')
.onDelete('CASCADE')
//USUARIO FK
table.integer('id_usuario_fk')
.unsigned()
.references('id')
.inTable('users')
.onUpdate('CASCADE')
.onDelete('CASCADE')
table.timestamps()
})
}
down () {
this.drop('notas')
}
}
module.exports = NotaSchema
我的模特
class Nota extends Model {
usuario () {
return this.belongsTo('App/Models/User')
}
fornecedor () {
return this.belongsTo('App/Models/Fornecedor')
}
}
我的控制器(添加寄存器)
const nota = new Nota();
nota.numero = request.input('numero')
nota.id_fornecedor_fk= request.input('fornecedor')
nota.id_usuario_fk = auth.user.id
try {
await nota.save()
return response.redirect('/notas')
}
catch(err) {
console.log(err)
}
我的 SearchController 进行查询
async searchpordata({request, response, session, view}) {
let query2 = request.input('data')
const notas = await Database
.select('notas.id', 'numero', 'notas.created_at', 'fornecedors.nome', 'fornecedors.cnpj', 'username')
.from('notas')
.where('notas.created_at', query2)
.join('fornecedors', 'fornecedors.id', 'id_fornecedor_fk')
.join('users', 'users.id', 'id_usuario_fk').first()
if(notas)
{
console.log(notas)
return view.render('resultado_notapordata',{
notas: notas,
})
}
else {
return view.render('resultado_buscanota_erro')
}
}
我尝试请求时的路线
http://127.0.0.1:3333/buscarpordata?data=2020-07-11
所以找不到任何寄存器
解决方案
首先,列的定义是什么created_at
?换句话说,存储的类型是什么?
其次,您的问题可能更多是将日期与时间戳进行比较,然后是格式之一:
select '2020-07-10'::timestamp;
timestamp
---------------------
07/10/2020 00:00:00
select '2020-07-10'::date = 'Fri Jul 10 2020 08:41:12-0300'::timestamp;
?column?
----------
f
--When you do the above the date gets changed to timestamp form to compare
--against the timestamp value.
select 'Fri Jul 10 2020 08:41:12-0300'::date;
date
------------
07/10/2020
select '2020-07-10'::date = 'Fri Jul 10 2020 08:41:12-0300'::date;
?column?
----------
t
(1 row)
--Now they are both being compared as dates.
推荐阅读
- ios - 确定屏幕上水平 CollectionView 单元格的可见性百分比
- fluentd - 修改record_splitter返回的记录,或者在调用记录拆分器之前准备好
- c# - '填充无效,无法删除。'
- networking - Windows 主机操作系统似乎忽略了通过 linux sendto 使用原始套接字注入的数据包
- android - 将 android 模拟器与 gsm usb 调制解调器连接
- snowflake-cloud-data-platform - 检索变量值并插入另一个表
- android - 以编程方式设置时,无法删除附加到 NavGraph 中不同 FragmentManager 的 Fragment
- mysql - mysql问题,无法添加缺少索引的外键约束
- chrome-ux-report - CrUX 社区仪表板中的 FP 是什么意思?
- r - x * 比例错误:二元运算符的非数字参数