2021年4月23日星期五

Equivalent of SQLite3 each() function in PostgreSQL Node.js server?

So I'm converting from database in a Node.js app from SQLite3 to PostgreSQL. Originally, I had my database equal to something like:

let db = new sqlite3.Database('./dbName.db', (err) => {     if (err) {       return console.error(err.message);     }     console.log("Connected to database");  });  

I was able to call db.each() on it when I wanted to query data from the table. In PostgreSQL, I'm setting up the database with:

const db = new Client({    user: 'myName',    host: 'hostName',    database: 'databaseName',    password: 'password',    port: ####,  });  

My query code for the original implementation (SQLite3) was part of an async function request:

function featureArrayHelper() {    return new Promise((resolve, reject) => {      let featureArray = []      let var4 = value4        db.each('SELECT rowid,* FROM table WHERE var4 > (?)', [passedInVar], (err, row) => {        if (err) {          reject(err)        } else {          let jsonObject = {            var1: row.var1,            var2 : row.var2,            var3 : row.var3,            var4 : row.var4,            var5: 0        }        featureArray.push(jsonObject)      }    }, (err, n) => {      if (err) {        reject(err)      } else {        resolve(featureArray)      }    });  })  }  

In this implementation, when I try to deploy the Node app to Heroku, I get an error on a line where I try to call db.each(), so what can I replace it with to retain the functionality established in the SQLite3 version?

https://stackoverflow.com/questions/67238084/equivalent-of-sqlite3-each-function-in-postgresql-node-js-server April 24, 2021 at 07:59AM

没有评论:

发表评论