0

I have this basic nodejs script:

var express = require('express'),
  Sequelize = require('sequelize'),
  promise = require('bluebird'),
  app = express(),
  optimus = new Sequelize('optimus', 'root', 'test', {host: '127.0.0.1', dialect: 'mysql'}),
  query = 'SELECT id FROM borrowers LIMIT 0,10',
  query2 = 'SELECT COUNT(*) FROM borrowers';

app.get('/', function(req,res) {

  var chain = new Sequelize.Utils.QueryChainer();

  console.log('begin');

  chain.add(optimus, 'query', [query,null,null,[]])
    .add(optimus, 'query', [query2,null,null,[]])
    .run()
    .success(function() {
      console.log('done');
    }).error(function(err) {
      console.log('oh no');
    });

  console.log('end');
  res.send('Hi Ma!');
});

var server = app.listen(3000, function() {
    console.log('Listening on port %d', server.address().port);
  }
);

Neither 'done' nor 'oh no' ever fires which leads me to believe that I can' chain raw queries in this manner.

What I'd really like to accomplish is to asynchronously resolve both queries and pass the results back via res.send().

I have to admit to being a complete n00b at nodejs so any insights into how to correctly structure this would be greatly appreciated.

1 Answer 1

4

The major issue with your code is the fact, that you are sending a response to the client/browser too early. Instead of res.send-ing at the end of the app.get method, you need to send the answer inside the success respectively inside the error callback. Here you are:

var express = require('express'),
  Sequelize = require('sequelize'),
  promise = require('bluebird'),
  app = express(),
  optimus = new Sequelize('sequelize_test', 'root', null, {host: '127.0.0.1', dialect: 'mysql'}),
  query = 'SELECT id FROM borrowers LIMIT 0,10',
  query2 = 'SELECT COUNT(*) as count FROM borrowers';

app.get('/', function(req,res) {

  var chain = new Sequelize.Utils.QueryChainer();

  console.log('begin');

  chain
    .add(optimus.query(query, null, { raw: true }))
    .add(optimus.query(query2, null, { raw: true, plain: true }))
    .run()
    .success(function(results) {
      res.send({
        resultOfQuery1: results[0],
        resultOfQuery2: results[1]
      });
    }).error(function(err) {
      console.log('oh no', err);
    });
});

var server = app.listen(3000, function() {
    console.log('Listening on port %d', server.address().port);
  }
);

Please notice, that I changed the credentials to my local ones. Furthermore also check the arguments of chain.add. Instead of passing the values for an upcoming serial executation, we just throw the actual asynchronous methods into it and let the querychainer handle their promises.

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

1 Comment

So you ran this locally and it worked for you as described? In my example I could not get it to run console.log('done'); at all.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.