首页 > 解决方案 > Node.js 和 PostgresSQL 与 Express(插入查询)

问题描述

我编写了一个代码来使用 express 和 NODE.js 从 postgres 数据库中获取数据。数据库已与后端代码连接,但仍以 json 格式显示错误。请检查我得到的代码和错误:错误就像: { "name": "error", "length": 120, "severity": "ERROR", "code": "42703", "position": "60", "file": "src\\backend\\parser\\parse_relation.c", "line": "2892", "routine": "errorMissingColumn" }

我写的代码是:

pg.connect(connectionString, function(err, client, done) {
        if (err) {
            console.error("Not able to get Connection " + err);
            res.status(400).send(err);
        } else {
            console.log("Connected.!");
        }

        // Insert Data in logintable query
        const
        insertQuery = {
            // give the query a unique name
            name : 'insert-user-details',
            text : 'INSERT INTO "testSchema".logintable (id, name) VALUES (03, "Anything")',
        }

        client.query(insertQuery, function(err, result) {
            done(); // closing the connection;
            if (err) {
                console.error("\nError : " + err);
                res.status(400).send(err);
            } else {
                console.log("1 record inserted");
                res.status(200).send(result.rows); // this line returns only a
                                                    // table data in json
                                                    // format.
                // res.status(200).send(result); // this line returns the rows
                // also with the table and field information.
            }
        });

标签: node.jspostgresqlexpress

解决方案


我得到了上面的答案,它得到了工作。在这里检查:

//Insert Query 
        client.query('INSERT INTO "testSchema".logintable (id, name) VALUES ($1, $2)', [06, 'Ganesh'], function(err, result) {
                if (err) {
                    console.error("\nError : " + err);
                    res.status(400).send(err);
                } else {
                    console.log("1 record inserted");
                    res.status(200).send(result.rows);
                }
            });

推荐阅读