javascript - Node JS+PostgreSQL : INSERT has more target columns than expressions

标签 javascript sql node.js postgresql

我想在每个新用户注册时用 CURRENT_TIMESTAMPINSERT 当前时间戳。列号等于 VALUES 中参数的数量。然而我得到 INSERT has more target columns than expressions。使用 node-postgres npm 模块作为 Controller 。

                         //Just 3 parameters, timestamp is hardcoded in the query
exports.create = function (username, email, password) {
    DB.connect(connection, function (err, client, done) {
        var query = client.query(

    //4 columns 
    "INSERT INTO users (username, email, userpass, datecreated) VALUES" +
    //4 parameters
    "(" + "'" + username + "'" + "," + "'" + email + "'" + "," + "'" + password + "'" + "'CURRENT_TIMESTAMP')");
        query.on('error', function (error) {
            console.log("query returned an " + error);
        });
        query.on('row', function (row, result) {
            result.addRow(row);
        });
    });
};

最佳答案

密码后缺少逗号且 current_Timestamp 周围没有任何符号

"INSERT INTO users (username, email, userpass, datecreated) VALUES" +
    //4 parameters
    "(" + "'" + username + "'" + "," + "'" + email + "'" + "," + "'" + password + "," + "'CURRENT_TIMESTAMP)"

--- 虽然这可能是解决当前问题的公认答案,但我强烈建议对 Craig 和 Lars 的答案进行评估。使用参数是一种更好的长期方法,因为它更安全;一旦您了解了如何以及正确的现代范例,实际上更容易编码。

关于javascript - Node JS+PostgreSQL : INSERT has more target columns than expressions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37679959/

相关文章:

javascript - Django、HTML、JS : <img src ="#"> work in HTML but in JS file not

javascript - 找到最小的 - Codewars 挑战 - Javascript

php - SELECT QUERY LIKE 仅包含字符串中的特定单词

MySQL - 行到列

javascript - 我在我的 Node.js 应用程序中使用 MongoSkin,在一段时间不活动后,它失去了与数据库的连接

javascript - 如何处理 Node js 中与内存分配相关的危机?

javascript - 当您在方法上使用编辑器时,TinyMCE 更新工具栏(初始化后)

javascript - 在 Javascript 中使用 AES 加密文本,然后在 C# WCF 服务中解密

sql - 100+ table 加入

node.js - 使用 Angular 和 Nodejs 的 Websocket : How to send data to specific client