【发布时间】:2021-06-26 19:16:40
【问题描述】:
所以我已经将我的服务器部署到 Heroku 了:
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
const http = require('http').Server(app);
const io = require('socket.io')(http);
我有一个函数向我的 Raspberry Pi(在本例中充当套接字客户端)发送请求,要求它运行抓取功能并相应地更新 Heroku Postgres 数据库中的表。唯一的问题是在 Pi 上,client.query() 函数无法识别......不知道为什么。这是我向 Pi 发送请求的函数:
async function F1() {
const client = await pool.connect();
try {
await client.query('BEGIN')
//do some database updating here in some table updates
let date = ("0" + terminationTime.getDate()).slice(-2);
let month = ("0" + (terminationTime.getMonth() + 1)).slice(-2);
let year = terminationTime.getFullYear();
let hours = terminationTime.getHours();
let minutes = terminationTime.getMinutes();
let seconds = terminationTime.getSeconds();
let timestamp = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds
let data = {
pool: pool, //here I tried passing in the pool, also tried passing in client
query: query,
timestamp: timestamp
}
io.to(RPiMiniServerID).emit("requestToPi", data)
} catch(err) {
console.log(err);
}
}
这是我在 Pi/Client 端的代码:
const io = require("socket.io-client");
const socket = io.connect("MY HEROKU APP URL HERE")
const { Pool, Client } = require("pg");
socket.on("requestToPi", (data) => sync function() {
let client = await data.pool.connect()
let query = data.query
let timestamp = data.timestamp
//a bunch more code and THEN
await client.query('INSERT INTO table (columnnames) VALUES($1)', [value]); //this is the line with the error
}());
我试过直接传入客户端,但这也行不通。不太确定在这里做什么 - 非常感谢任何帮助!
【问题讨论】:
标签: javascript node.js postgresql sockets socket.io