Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I return the results of a query using Sequelize and Javascript?

I'm new at javascript and I've hit a wall hard here. I don't even think this is a Sequelize question and probably more so about javascript behavior.

I have this code:

sequelize.query(query).success( function(row){
            console.log(row);
    }
)

The var row returns the value(s) that I want, but I have no idea how to access them other than printing to the console. I've tried returning the value, but it isn't returned to where I expect it and I'm not sure where it goes. I want my row, but I don't know how to obtain it :(

like image 294
user2352919 Avatar asked Jan 14 '23 11:01

user2352919


2 Answers

Using Javascript on the server side like that requires that you use callbacks. You cannot "return" them like you want, you can however write a function to perform actions on the results.

sequelize.query(query).success(function(row) {
    // Here is where you do your stuff on row
    // End the process
    process.exit();
}

A more practical example, in an express route handler:

// Create a session
app.post("/login", function(req, res) {
    var username = req.body.username,
        password = req.body.password;
    // Obviously, do not inject this directly into the query in the real
    // world ---- VERY BAD.
    return sequelize
      .query("SELECT * FROM users WHERE username = '" + username + "'")
      .success(function(row) {
          // Also - never store passwords in plain text
          if (row.password === password) {
              req.session.user = row;
              return res.json({success: true});
          }
          else {
              return res.json({success: false, incorrect: true});
          }
      });
});

Ignore injection and plain text password example - for brevity.

Functions act as "closures" by storing references to any variable in the scope the function is defined in. In my above example, the correct res value is stored for reference per request by the callback I've supplied to sequelize. The direct benefit of this is that more requests can be handled while the query is running and once it's finished more code will be executed. If this wasn't the case, then your process (assuming Node.js) would wait for that one query to finish block all other requests. This is not desired. The callback style is such that your code can do what it needs and move on, waiting for important or processer heavy pieces to finish up and call a function once complete.

EDIT

The API for handling callbacks has changed since answering this question. Sequelize now returns a Promise from .query so changing .success to .then should be all you need to do.

like image 66
Brandon Buck Avatar answered Jan 29 '23 21:01

Brandon Buck


According to the changelog

Backwards compatibility changes:

Events support have been removed so using .on('success') or .success() is no longer supported. Try using .then() instead.

According this Raw queries documentation you will use something like this now:

sequelize.query("SELECT * FROM `users`", { type: sequelize.QueryTypes.SELECT})
    .then(function(users) {
        console.log(users);
    });
like image 40
Hamza Ouaghad Avatar answered Jan 29 '23 19:01

Hamza Ouaghad