0

When I run the server there are no error. But when I visit the site it starts to give out an error TypeError: Converting circular structure to JSON

my database.js

var mysql      = require('mysql');

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

module.exports = {
  connection: connection
}

My products.js

var db = require('../database');

function getProducts(request) {
    var product = db.connection.query('SELECT * from products', function(err) {
        // connected! (unless `err` is set)
    });

    request.reply(product);
}

I just started with node.


update

db.connection.query('SELECT * from products', function(err, results) {
    if (err) throw err;
    console.log(err);

    var products = results;
});

returning null on console.


update

var query = db.connection.query('SELECT * from products;', function(error, rows, fields)     {
    console.log(rows);
    var products = rows;
});

it seems like adding ; to the end of the query did it.


Another thing is now products is not defined

function getProducts(request) {

    if (request.query.name) {
        request.reply(findProducts(request.query.name));
    }
    else {
        request.reply(products);
    }
}

As for the answer of the last question:

var products = query._results;
majidarif
  • 18,694
  • 16
  • 88
  • 133
  • Looks like you have a problem with the actual data being returned. Do `console.log(err);` inside your connect query and see what it outputs. – brandonscript Jan 12 '14 at 06:05
  • Hrm... OK, you need to figure out what line of code is generating that TypeError. I'd guess that it's having trouble returning the mysql data as JSON. – brandonscript Jan 12 '14 at 06:14
  • @r3mus I think i got it. i updated the question. – majidarif Jan 12 '14 at 06:19
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) as the issue lies in the handling of asynchronous data. – Paul Mougel Jan 12 '14 at 08:48

0 Answers0