Update postgreql database with Node.js

ぐ巨炮叔叔 提交于 2020-01-06 06:36:29

问题


I try to Update a table on postgresql so for that i'm creating two arrays

    var name_ = []; 
    var id_ = [];

then i creat a forEach loop where MyRow is the query that contain different data

    MyRow.rows.forEach(function(row){ 
      name_.push(row.name);
      id_.push(row.id);                      
    });            
    var updateAccount = 'UPDATE myTable SET myRow =$1 '+
    'where id = $2'   
    client.query(updateAccount,[name_.toString(),id_.toString()],false).then(function(err,result){                      
        if (err) {
            console.log(err.stack);        
          } else {
            console.log(name_)
          }
    });

then i had an error error: missing FROM-clause entry for table "myTable "


回答1:


I'm not sure the error is related, but where is shema mentioned? Also use backticks, oneLine and const on template strings.

const updateAccount = oneLine`
   UPDATE myTable
   SET myRow = $1
   WHERE shema.id = $2  -- where is `shema` defined?
`;



回答2:


My guess is that you should enclose your values in single quotes.

var updateAccount = "UPDATE myTable SET myRow = '$1' "+
"where shema.id = '$2'"

In your example, your values are interpreted as column names, so PostgreSQL asks you to specify a table containing these.



来源:https://stackoverflow.com/questions/49150477/update-postgreql-database-with-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!