Nasser
Nasser

Reputation: 2140

Node JS: Convert retrieved data from DB as JSON to Plain text

In the following code, data are retrieved from a database into JSON. What I would like to do is to display each single data as a plain text:

var http = require("http");
var mysql = require('mysql');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');

var urlencodedParser = bodyParser.urlencoded({ extended: false })

    app.use(express.static('public'));

    app.get('/Search.html', function (req, res) {
       res.sendFile( __dirname + "/" + "Search.html" );
    })

   var connection = mysql.createConnection(
    {
      host     : 'localhost',
      user     : 'root',
      password : 'passpass',
      database : 'SocialQuery',
    }
    );

connection.connect();

app.post('/process_post', urlencodedParser, function (req, res) {

   // Prepare output in JSON format
   response = {
       SearchType:req.body.SearchTypes,
       Term:req.body.term
   };
    //var vas = JSON.stringify(response);
    var search = req.body.SearchTypes;
    var term = req.body.term;

    var queryString;
    if(search == 'Author')
    {
        queryString = 'Select Label,TDate from Tweet where AuthorID IN (select ID from Author where Lable = ?)';
    }
    else if(search == 'Mention')
    {
        queryString = 'select Tweet.Label, Tweet.TDate, Author.Lable from Tweet, Author where Tweet.ID IN (select TweetID from TweetMention where MentionID IN (select ID from Mention where Label = ?)) AND Author.ID = Tweet.AuthorID'
    }
    var query = connection.query(queryString, [term], function(err, rows) {
        console.log(rows);
        var tweet = JSON.stringify(rows);
        res.write("Author: " + tweet.Lable);
        res.write("Date: " + tweet.TDate);
        res.end();
    });
    console.log(query.sql);
})

//}).listen(8081);
http.createServer(app).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');

When I print the data using res.write(JSON.stringify(rows)); I got the following:

[{"Label":"lest play hero","TDate":"2016-03-12T00:00:00.000Z","Lable":"esti_jony"},{"Label":"u r the best ! Ill always keep","TDate":"2016-03-08T00:00:00.000Z","Lable":"adam03cooper03"}]

but when I run the code above, I got:

Author: undefined Date: undefined

What I understood is the problem because two rows of data have been retrieved and I do not know how to let it display each author (Lable) and each date (TDate).

Upvotes: 0

Views: 1055

Answers (1)

mscdex
mscdex

Reputation: 106696

You're converting a javascript object to a JSON string and then trying to access the javascript object's properties on the string. That won't work. You want to use the javascript object instead, but since there is more than one row, you will have to either choose which one you want to respond with or write all rows or whatever your use case calls for.

For example:

var query = connection.query(queryString, [term], function(err, rows) {
    if (err) throw err;
    for (var i = 0; i < rows.length; ++i) {
      var tweet = rows[i];
      res.write("\nAuthor: " + tweet.Lable);
      res.write("\nDate: " + tweet.TDate);
      if (i + 1 < rows.length)
        res.write('\n');
    }
    res.end();
});

Upvotes: 1

Related Questions