1

I'm building an auction house. I'm using the nodejs-mysql package and I have this nodejs server.js-code for the main page, where multiple auctions are displayed:

// loop
var pollingLoop = function() {
  // Doing the database query
var query = connection.query('SELECT a.`id`,a.`product_id`,a.`price`,a.`random`,a.`price_end`,TIMESTAMPDIFF(SECOND,NOW(),a.`date_end`) AS duration,p.`price_retail` FROM `auctions` AS a LEFT JOIN `products` AS p ON p.`id` = a.`product_id` WHERE TIMESTAMPDIFF(SECOND,NOW(),a.`date_end`) > "-1" ORDER BY `duration` DESC,`id` DESC'),
    auctions = []; 
  1. How can I accomplish that this query is changed when a $_GET['id'] is available to another SELECT-statement with a WHERE-condition? Because I want to add WHERE auction='$_GET['id']' for a single auction then (but server-side).
  2. Is there a way to make it more readable? Because when I press ENTER the SELECT-statement won't work at all. This is how it looks on the nodejs-server-file server.js: https://i.sstatic.net/37fDl.jpg

1 Answer 1

1

There are two ways to break up a Javascript string into multiple lines: Backslashing and string concatenation.

Delimiting with backslashes

var text = "Lorem ipsum dolor sit amet,\
consectetur adipisicing elit,\
sed do eiusmod tempor incididunt\
ut labore et dolore magna aliqua.";

Delimiting with string concatenation

var text = "Lorem ipsum dolor sit amet,"
         + "consectetur adipisicing elit,"
         + "sed do eiusmod tempor incididunt"
         + "ut labore et dolore magna aliqua.";

You need to build your query dinamically. Something like this.

var query = 'SELECT \
    a.`id`, \
    a.`product_id`, \
    a.`price`, \
    a.`random`, \
    a.`price_end`, \
    TIMESTAMPDIFF(SECOND, \
        NOW(), \
        a.`date_end`) AS duration, \
    p.`price_retail` \
FROM \
    `auctions` AS a \
        LEFT JOIN \
    `products` AS p ON p.`id` = a.`product_id`';

var where = '';
if (condition) {
   where = 'WHERE \
    TIMESTAMPDIFF(SECOND, \
        NOW(), \
        a.`date_end`) > - 1';
} else {
   where = "auction='$_GET['id']'";
}

var order = ' ORDER BY `duration` DESC , `id` DESC';
query = query + where + order 
var res = connection.query(query), auctions = []; 

p.s. This is just an idea. For example, you can use var where = array() and fill it with conditions. Then, just loop over array and create query joining conditions by AND.

upd:

If you don't use express module, you can find GET parameters using url module.

var query = require('url').parse(req.url,true).query;

var id = query.id;
var some_par = query.some_par;

For url: /path/filename?id=123&some_par=456

Then you can build query with id variable.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. Now I'm trying just to get the id, but my result is "undefined" than. With that code: var qry = require('url').parse(socket.handshake.url); var qry_id = qry.id;
Don't forget TRUE - .parse(socket.handshake.url, true); What you get console.log(qry) ?
Yes, I already added "true". It says [object Object] then. But if I type console.log(qry.path) the whole url shows. But how can I just get a specific query string like id? And why is he showing the socket.io url and not the url of my localhost?
localhost:3001/?id=456789 - var qry = require('url').parse(req.url, true).query; - console.log(qry.id); - this works for me fine.
I just found out the issue. It's about the folder. In server.js it's defined as http.listen(1337). But I'm not testing on http://localhost:1337. I testing on http://localhost/customname/. So that's why I dont get an id. But how can I change it to the customname folder that for example http://localhost/customname/?id=1234 would work too?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.