postgresql - 我如何在没有任何库的情况下使用 javascript 创建一个带有时区的日期来保存到 postgres
问题描述
我正在尝试将带有时区的日期保存到我的 postgress 数据库中。使用时出现以下错误created_at = Date.now()
{"errors":[{"extensions":{"path":"$.selectionSet.insert_users.args.objects","code":"data-exception"},"message":"date/time field value out of range: \"1588130621916\""}]}
这是代码,我将 asterix 放在与问题无关的代码上。
function (user, context, callback) {
const userId = user.user_id;
const email = user.email;
let name;
const created_at = Date.now();
console.log(created_at);
if (user.name) {
name = user.name;
} else {
name = user.user_metadata.full_name;
}
const admin_secret = "********";
const url = "*******/v1/graphql";
request.post({
headers: {'content-type' : 'application/json', 'x-hasura-admin-secret': admin_secret},
url: url,
body: `{\"query\":\"mutation($id: String!, $name: String, $created_at: timestamptz!, $email: String!) {\\n insert_users(\\n objects: [{ id: $id, name: $name, created_at: $created_at, email: $email, username: $id }]\\n on_conflict: {\\n constraint: users_pkey\\n update_columns: [updated_at, name]\\n }\\n ) {\\n affected_rows\\n }\\n }\",\"variables\":{\"id\":\"${userId}\",\"name\":\"${name}\", \"created_at\":\"${created_at}\",\"email\":\"${email}\"}}`
}, function(error, response, body){
console.log(body);
callback(null, user, context);
});
}
解决方案
Te sugeriria revisar que la columna en la base de datos soporte el dato de tipo fecha con time-zone ej mycolumn TIME with time-zone
Lo segundo seria chequear como recive el formato de hora tu end-point ejemlo 2012-04-23T18:25:43.511Z es el formato de fecha que se envia en un json。
const jsondate = JSON.stringify(new Date());//javascript-format
推荐阅读
- wso2 - 如何在 siddhi 聚合查询中使用“select concat(col)”?
- flask - Tiangolo烧瓶docker镜像配置不使用端口80
- mysql - 警告:mysqli_select_db() 期望参数 1 为 mysqli,字符串在第 34 行的 C:\xampp\htdocs\account\classes\class.sql.php 中给出
- java - 在 Java 赋值中使用数组和 for 循环时遇到问题
- command - Visual Studio 命令提示符无法正常工作
- c# - 在整个系统中递归查找文件,返回第一个文件夹
- regex - 带有正则表达式的wpf Richtextbox选择
- javascript - Jquery/Javascript:倒数计时器动作不规律
- laravel - 部署后服务器上的 Laravel 本地路由
- typescript - 检测到函数返回“void”(而不是“undefined”)