我如何从数据库中选择一个值,然后使用Postgresql将其插入另一个数据库?

时间:2019-12-04 04:55:35

标签: javascript postgresql express

我有以下代码:

app.post("/enroll", async (req,res) => { 
    const title = req.body.title;
    const date = req.body.date;
    const location = req.body.location;
    const username = req.body.username;

    try {
        // find id for the camp
        const id = 'SELECT id FROM workshop WHERE title=$1 AND date=$2 AND location=$3';
        const foundId = await pool.query(id,[title,date,location]);
        console.log(foundId.rows);

            const template7 = "INSERT INTO enroll (id,username) VALUES ($1,$2)";
            const response7 = await pool.query(template7, [foundId.rows,username]);
            res.json({"status":"user added"});
        }
    } catch (err) {
        console.log(err);
    }
});

我有一个数据库,该数据库正在插入一个人的ID和用户名。我让用户输入研讨会的标题,日期和位置以及用户名。然后,我找到该工作坊的ID,因此可以将该ID和用户名插入另一个名为enroll的数据库中。我有这段代码,但它给了我以下错误:

error: invalid input syntax for integer: "{"{\"id\":4}"}"

该ID显示为[{id:4}]。

这是我的数据库:

CREATE TABLE workshop (
id SERIAL PRIMARY KEY,
title TEXT,
date DATE,
location TEXT,
maxseats INTEGER,
instructor TEXT
);

CREATE TABLE enroll (
id INTEGER REFERENCES workshop(id),
username TEXT REFERENCES users(username),
PRIMARY KEY (id,username));

反正我可以从研讨会数据库中选择ID号并将其插入注册吗?

0 个答案:

没有答案