Group Group Group Group Group Group Group Group Group

sendPushes.js I get TypeError: Cannot read property 'rows' of undefined and Connection terminated unexpectedly

#1

I have installed nodejs to run and test sendPushes.js but I have a problem is not connected and I get this error stack. also I ran the script in VS code

TypeError: Cannot read property ‘rows’ of undefined
at Query.callback (/Users/abdalla.mohamed/Documents/SandBox/PushNotifications/Sources/Appjs/main.js:40:22)
at Query.handleError (/Users/user/node_modules/pg/lib/query.js:142:17)
at /Users/user/node_modules/pg/lib/client.js:63:13
at processTicksAndRejections (internal/process/task_queues.js:82:9)
and
Error: Connection terminated unexpectedly
at Connection. (/Users/user/node_modules/pg/lib/client.js:235:9)
at Object.onceWrapper (events.js:288:20)
at Connection.emit (events.js:200:13)
at Socket. (/Users/user/node_modules/pg/lib/connection.js:77:10)
at Socket.emit (events.js:200:13)
at TCP. (net.js:586:12)

code

client.query('SELECT DISTINCT token FROM tokens WHERE debug = true', (err, res) => {
  console.log("error"+ err ? err.stack : res.rows[0].message) 
 

  console.log(err ? err.stack : res.rows[0].message) 

  const tokens = res.rows.map(row => row.token)

  apnProvider.send(note, tokens).then( (response) => {
     if(response.sent == true)//has successful pushes
     {
       console.log(response.sent)
     }
     else if (response.failed == true)
     {
      console.log(response.failed)
     }
    // response.failed has error details
  });
  client.end()
})
#2

Unfortunately I’m not a node expert, but looking at your error it would appear that res variable isn’t being set, and so you don’t have any rows available. Sounds like you’re having an issue talking to your SQL server from the node script.