mardi 26 janvier 2016

How do you put a SQLite index into a variable with Nodejs?

I am using express and nodejs to make a chatroom, and I have the messages saved in a sql .db file. The indices have an integer primary key that is autoincrimented, with 3 more columns of text information: room, nickname, and body. Here's some code where I try and debug:

app.post('/messages', function(request, response){
if(request.body.username == ''){
    var username = "Anonymous";
}

else {
var username = request.body.username;
}
var msg = request.body.msg;
conn.query('INSERT INTO messages(id, room, nickname, body) VALUES ($1, $2, $3, $4);', [n, room1, username, msg]); //room, name, text

var out = conn.query('SELECT body FROM messages WHERE id = 2');
console.log(out);

});

This outputs a bunch of settings for the table

SQLite3Query {
  _readableState:
   ReadableState {
     objectMode: true,
     highWaterMark: 16,
     buffer: [],
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: null,
     ended: false,
     endEmitted: false,
     reading: false,
     sync: true,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     defaultEncoding: 'utf8',
     ranOut: false,
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },
  readable: true,
  domain: null,
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  text: 'SELECT body FROM messages WHERE id = 2',
  _fields: null,
  _result: { rows: [] },
  _errored: false,
  values: [],
  callback: undefined }

The final part "WHERE id = 2" confuses me a bit, because if I put the normal '' around the 2 it breaks.

Using other software, I can see that the database does have information in it. Eventually I would like to have the chatroom print out all the indices from the proper room into HTML, but this hurdle exists.

Aucun commentaire:

Enregistrer un commentaire