node.js - 为什么不能将嵌套 JSON 数据 gps 作为未知类型插入?

标签 node.js postgresql

我正在尝试将数据从客户端插入到服务器的 PostQSL 数据库中。 但是,有一个错误:

服务器错误:

uid: test id
gps: 111
(node:8607) UnhandledPromiseRejectionWarning: error: could not determine data type of parameter $2

为什么我不能将嵌套 JSON 数据 gps 作为未知类型插入?

客户端(角度):

  tracking_info = {
       uid: "test id",
       gps: {
         logitude: 111,
         latitude: 222,
         timestamp: 12345,
       }
     }

  // Send data to server
  api.post('savemessage', tracking_info).share();

服务器(Node.js)

    ...
    console.log("uid: "+ uid);
    console.log("gps: "+ gps.latitude);

    // insert a new message
    await client.query('INSERT INTO messages(uid, gps) VALUES($1, $2)', [uid, gps])
    ...

PostSQL 表设置:

[Name]        [DataType]
 uid           VARCHAR
 gps           UNKNOWN

最佳答案

使用 JSON 数据类型来存储 JSON 数据。

  CREATE TABLE tracking_info (  
    uid UUID NOT NULL,
    gps JSON
  );

您可能需要执行 JSON.stringify。

await client.query('INSERT INTO messages(uid, gps) VALUES($1, $2)', [uid, JSON.stringify(gps)])

阅读更多详情 http://www.postgresqltutorial.com/postgresql-json/

关于node.js - 为什么不能将嵌套 JSON 数据 gps 作为未知类型插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52069944/

相关文章:

node.js - ElasticSearch标准Node客户端可安全用于云功能吗?

mongodb - 用于存储非关系数据的 AWS 数据库解决方案

sql - 我如何请求 Postgres 获取特定时间序列中不同日期的计数?

ruby-on-rails - 找出 Heroku 当前使用的 DB

javascript - 创建 react 应用程序 : How do I "npm start" with a specific browser?

node.js - 在 Azure 的 IISNode 上调用本地主机 URL

javascript - 测试后连接未关闭?

python - 无法使用python从数据库中选择一千万行

sql - 创建引用另一个表变量的表的正确方法

node.js - 无法通过 electron-builder 自定义 NSIS 包含脚本注册和注销 .dll 文件