0

i want to get result: group + count of messages which belong to the current group for example:

webdevelopment - 34 messages

How can I achieve the same result without performing _.each

how i can to get the result one Query I used MySql, my code something like this

 Group.find().exec(function (err, groups) {
        if (err)
            return next(err);
        _.each(groups, function (grouper) {
            Messages.count({
                groupId : grouper.groupId,
            }).exec(function (err, found) {
                console.log(grouper.groupName + ' - ' + found + ' messages.');

            });
        });
    });

my module somethink like this

module messages

module.exports = {
  attributes: {
    id:{
         type: 'integer',
         primaryKey: true,
         autoIncrement: true
      },
    groupId:{
        type:'int'
    }
  }
};

module Group

module.exports = {
  attributes: {
  id:{
     type: 'integer',
     primaryKey: true,
     autoIncrement: true
  },
  groupName:{
    type:'string'
  }

  }
};

2 Answers 2

2

You need to write the SQL query that gives the desired result. Lets say the query is:

var myQuery = "Select ... from ...";

Now you can execute the query using query method of the Model like so:

Group.query(myQuery, function(err, res) {
  /* use res.rows to get the result */
});

It does not matter which model you use when using query method.

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

Comments

1

As of Sails v1, Model.query has been deprecated, instead use sails.sendNativeQuery(queryString, paramArray)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.