How to return data of sequelize js from Meteor Method?

I would like to use Sequelize Js within Meteor.
But I don’t know how to return data for method.

// Server
import Sequelize from 'sequelize';
var sequelize = new Sequelize('meteor', 'sa', '123', {
    host: '192.168.0.109',
    dialect: 'mssql',

    pool: {
        max: 5,
        min: 0,
        idle: 10000
    },
});

var User = sequelize.define('user', {
    firstName: {
        type: Sequelize.STRING
    },
    lastName: {
        type: Sequelize.STRING
    }
});

-----------------
Meteor.methods({
    'getData'(){
   
        User.findAll().then(function (user) {
              return user
        });

   }
});
Meteor.methods({
  async getData() {
    return await User.findAll();
 }
});

Still don’t return!
How to solve?

First, make sure the findAll is working:

Meteor.methods({
  getData() {
    console.log (User.findAll());
  }
});

Yes, it can get

// Console
Executing (default): SELECT [id], [firstName], [lastName], [createdAt], [updatedAt] FROM [users] AS [user];
I20170119-22:03:20.474(7)? [ { dataValues:
I20170119-22:03:20.475(7)?      { id: 3,
I20170119-22:03:20.475(7)?        firstName: 'Theara Update',
I20170119-22:03:20.477(7)?        lastName: 'Yuom',
I20170119-22:03:20.477(7)?        createdAt: Thu Jan 19 2017 20:13:36 GMT+0700 (ICT),
I20170119-22:03:20.478(7)?        updatedAt: Thu Jan 19 2017 20:13:36 GMT+0700 (ICT) },
I20170119-22:03:20.479(7)?     _previousDataValues:
I20170119-22:03:20.479(7)?      { id: 3,
I20170119-22:03:20.479(7)?        firstName: 'Theara Update',
I20170119-22:03:20.480(7)?        lastName: 'Yuom',
I20170119-22:03:20.480(7)?        createdAt: Thu Jan 19 2017 20:13:36 GMT+0700 (ICT),
I20170119-22:03:20.480(7)?        updatedAt: Thu Jan 19 2017 20:13:36 GMT+0700 (ICT) },
I20170119-22:03:20.481(7)?     _changed: {},
I20170119-22:03:20.481(7)?     '$modelOptions':
I20170119-22:03:20.482(7)?      { timestamps: true,
I20170119-22:03:20.482(7)?        instanceMethods: {},
I20170119-22:03:20.482(7)?        classMethods: {},
I20170119-22:03:20.482(7)?        validate: {},
I20170119-22:03:20.483(7)?        freezeTableName: false,
I20170119-22:03:20.483(7)?        underscored: false,
I20170119-22:03:20.483(7)?        underscoredAll: false,
I20170119-22:03:20.484(7)?        paranoid: false,
I20170119-22:03:20.484(7)?        rejectOnEmpty: false,
I20170119-22:03:20.484(7)?        whereCollection: null,
I20170119-22:03:20.485(7)?        schema: null,
I20170119-22:03:20.485(7)?        schemaDelimiter: '',
I20170119-22:03:20.486(7)?        defaultScope: {},
I20170119-22:03:20.486(7)?        scopes: [],
I20170119-22:03:20.486(7)?        hooks: {},
I20170119-22:03:20.487(7)?        indexes: [],
I20170119-22:03:20.487(7)?        name: [Object],
I20170119-22:03:20.487(7)?        omitNul: false,
I20170119-22:03:20.487(7)?        sequelize: [Object],
I20170119-22:03:20.488(7)?        uniqueKeys: {},
I20170119-22:03:20.488(7)?        hasPrimaryKeys: true },
I20170119-22:03:20.488(7)?     '$options':
I20170119-22:03:20.489(7)?      { isNewRecord: false,
I20170119-22:03:20.489(7)?        '$schema': null,
I20170119-22:03:20.489(7)?        '$schemaDelimiter': '',
I20170119-22:03:20.489(7)?        raw: true,
I20170119-22:03:20.490(7)?        attributes: [Object] },
I20170119-22:03:20.490(7)?     hasPrimaryKeys: true,
I20170119-22:03:20.490(7)?     __eagerlyLoadedAssociations: [],
I20170119-22:03:20.490(7)?     isNewRecord: false },
I20170119-22:03:20.491(7)?   { dataValues:
I20170119-22:03:20.491(7)?      { id: 4,
I20170119-22:03:20.491(7)?        firstName: 'Narong',
I20170119-22:03:20.491(7)?        lastName: 'Sao',
I20170119-22:03:20.492(7)?        createdAt: Thu Jan 19 2017 20:12:38 GMT+0700 (ICT),
I20170119-22:03:20.492(7)?        updatedAt: Thu Jan 19 2017 20:12:38 GMT+0700 (ICT) },
I20170119-22:03:20.493(7)?     _previousDataValues:
I20170119-22:03:20.493(7)?      { id: 4,
I20170119-22:03:20.493(7)?        firstName: 'Narong',
I20170119-22:03:20.494(7)?        lastName: 'Sao',
I20170119-22:03:20.494(7)?        createdAt: Thu Jan 19 2017 20:12:38 GMT+0700 (ICT),
I20170119-22:03:20.494(7)?        updatedAt: Thu Jan 19 2017 20:12:38 GMT+0700 (ICT) },
I20170119-22:03:20.494(7)?     _changed: {},
I20170119-22:03:20.495(7)?     '$modelOptions':
I20170119-22:03:20.495(7)?      { timestamps: true,
I20170119-22:03:20.495(7)?        instanceMethods: {},
I20170119-22:03:20.496(7)?        classMethods: {},
I20170119-22:03:20.496(7)?        validate: {},
I20170119-22:03:20.497(7)?        freezeTableName: false,
I20170119-22:03:20.497(7)?        underscored: false,
I20170119-22:03:20.497(7)?        underscoredAll: false,
I20170119-22:03:20.499(7)?        paranoid: false,
I20170119-22:03:20.499(7)?        rejectOnEmpty: false,
I20170119-22:03:20.499(7)?        whereCollection: null,
I20170119-22:03:20.500(7)?        schema: null,
I20170119-22:03:20.500(7)?        schemaDelimiter: '',
I20170119-22:03:20.500(7)?        defaultScope: {},
I20170119-22:03:20.500(7)?        scopes: [],
I20170119-22:03:20.501(7)?        hooks: {},
I20170119-22:03:20.501(7)?        indexes: [],
I20170119-22:03:20.501(7)?        name: [Object],
I20170119-22:03:20.501(7)?        omitNul: false,
I20170119-22:03:20.502(7)?        sequelize: [Object],
I20170119-22:03:20.502(7)?        uniqueKeys: {},
I20170119-22:03:20.502(7)?        hasPrimaryKeys: true },
I20170119-22:03:20.503(7)?     '$options':
I20170119-22:03:20.503(7)?      { isNewRecord: false,
I20170119-22:03:20.503(7)?        '$schema': null,
I20170119-22:03:20.503(7)?        '$schemaDelimiter': '',
I20170119-22:03:20.504(7)?        raw: true,
I20170119-22:03:20.504(7)?        attributes: [Object] },
I20170119-22:03:20.504(7)?     hasPrimaryKeys: true,
I20170119-22:03:20.505(7)?     __eagerlyLoadedAssociations: [],
I20170119-22:03:20.506(7)?     isNewRecord: false },
I20170119-22:03:20.506(7)?   { dataValues:
I20170119-22:03:20.506(7)?      { id: 5,
I20170119-22:03:20.507(7)?        firstName: 'Sour',
I20170119-22:03:20.507(7)?        lastName: 'Kh',
I20170119-22:03:20.507(7)?        createdAt: Thu Jan 19 2017 20:13:18 GMT+0700 (ICT),
I20170119-22:03:20.507(7)?        updatedAt: Thu Jan 19 2017 20:13:18 GMT+0700 (ICT) },
I20170119-22:03:20.508(7)?     _previousDataValues:
I20170119-22:03:20.508(7)?      { id: 5,
I20170119-22:03:20.510(7)?        firstName: 'Sour',
I20170119-22:03:20.512(7)?        lastName: 'Kh',
I20170119-22:03:20.513(7)?        createdAt: Thu Jan 19 2017 20:13:18 GMT+0700 (ICT),
I20170119-22:03:20.513(7)?        updatedAt: Thu Jan 19 2017 20:13:18 GMT+0700 (ICT) },
I20170119-22:03:20.514(7)?     _changed: {},
I20170119-22:03:20.514(7)?     '$modelOptions':
I20170119-22:03:20.514(7)?      { timestamps: true,
I20170119-22:03:20.515(7)?        instanceMethods: {},
I20170119-22:03:20.515(7)?        classMethods: {},
I20170119-22:03:20.515(7)?        validate: {},
I20170119-22:03:20.516(7)?        freezeTableName: false,
I20170119-22:03:20.517(7)?        underscored: false,
I20170119-22:03:20.517(7)?        underscoredAll: false,
I20170119-22:03:20.518(7)?        paranoid: false,
I20170119-22:03:20.518(7)?        rejectOnEmpty: false,
I20170119-22:03:20.518(7)?        whereCollection: null,
I20170119-22:03:20.518(7)?        schema: null,
I20170119-22:03:20.519(7)?        schemaDelimiter: '',
I20170119-22:03:20.519(7)?        defaultScope: {},
I20170119-22:03:20.519(7)?        scopes: [],
I20170119-22:03:20.519(7)?        hooks: {},
I20170119-22:03:20.520(7)?        indexes: [],
I20170119-22:03:20.520(7)?        name: [Object],
I20170119-22:03:20.520(7)?        omitNul: false,
I20170119-22:03:20.521(7)?        sequelize: [Object],
I20170119-22:03:20.521(7)?        uniqueKeys: {},
I20170119-22:03:20.522(7)?        hasPrimaryKeys: true },
I20170119-22:03:20.523(7)?     '$options':
I20170119-22:03:20.523(7)?      { isNewRecord: false,
I20170119-22:03:20.524(7)?        '$schema': null,
I20170119-22:03:20.524(7)?        '$schemaDelimiter': '',
I20170119-22:03:20.524(7)?        raw: true,
I20170119-22:03:20.525(7)?        attributes: [Object] },
I20170119-22:03:20.525(7)?     hasPrimaryKeys: true,
I20170119-22:03:20.527(7)?     __eagerlyLoadedAssociations: [],
I20170119-22:03:20.529(7)?     isNewRecord: false },
I20170119-22:03:20.529(7)?   { dataValues:
I20170119-22:03:20.530(7)?      { id: 6,
I20170119-22:03:20.531(7)?        firstName: 'Yiman',
I20170119-22:03:20.531(7)?        lastName: 'VN',
I20170119-22:03:20.531(7)?        createdAt: Thu Jan 19 2017 20:13:53 GMT+0700 (ICT),
I20170119-22:03:20.531(7)?        updatedAt: Thu Jan 19 2017 20:13:53 GMT+0700 (ICT) },
I20170119-22:03:20.532(7)?     _previousDataValues:
I20170119-22:03:20.532(7)?      { id: 6,
I20170119-22:03:20.532(7)?        firstName: 'Yiman',
I20170119-22:03:20.533(7)?        lastName: 'VN',
I20170119-22:03:20.533(7)?        createdAt: Thu Jan 19 2017 20:13:53 GMT+0700 (ICT),
I20170119-22:03:20.536(7)?        updatedAt: Thu Jan 19 2017 20:13:53 GMT+0700 (ICT) },
I20170119-22:03:20.536(7)?     _changed: {},
I20170119-22:03:20.537(7)?     '$modelOptions':
I20170119-22:03:20.537(7)?      { timestamps: true,
I20170119-22:03:20.537(7)?        instanceMethods: {},
I20170119-22:03:20.538(7)?        classMethods: {},
I20170119-22:03:20.538(7)?        validate: {},
I20170119-22:03:20.538(7)?        freezeTableName: false,
I20170119-22:03:20.539(7)?        underscored: false,
I20170119-22:03:20.539(7)?        underscoredAll: false,
I20170119-22:03:20.539(7)?        paranoid: false,
I20170119-22:03:20.539(7)?        rejectOnEmpty: false,
I20170119-22:03:20.539(7)?        whereCollection: null,
I20170119-22:03:20.540(7)?        schema: null,
I20170119-22:03:20.540(7)?        schemaDelimiter: '',
I20170119-22:03:20.540(7)?        defaultScope: {},
I20170119-22:03:20.540(7)?        scopes: [],
I20170119-22:03:20.540(7)?        hooks: {},
I20170119-22:03:20.540(7)?        indexes: [],
I20170119-22:03:20.541(7)?        name: [Object],
I20170119-22:03:20.541(7)?        omitNul: false,
I20170119-22:03:20.541(7)?        sequelize: [Object],
I20170119-22:03:20.541(7)?        uniqueKeys: {},
I20170119-22:03:20.541(7)?        hasPrimaryKeys: true },
I20170119-22:03:20.542(7)?     '$options':
I20170119-22:03:20.542(7)?      { isNewRecord: false,
I20170119-22:03:20.542(7)?        '$schema': null,
I20170119-22:03:20.542(7)?        '$schemaDelimiter': '',
I20170119-22:03:20.542(7)?        raw: true,
I20170119-22:03:20.543(7)?        attributes: [Object] },
I20170119-22:03:20.543(7)?     hasPrimaryKeys: true,
I20170119-22:03:20.543(7)?     __eagerlyLoadedAssociations: [],
I20170119-22:03:20.543(7)?     isNewRecord: false } ]

That doesn’t look like a Promise to me (and even if it is, Meteor will resolve it before return).

Have you tried:

Meteor.methods({
  getData() {
    return User.findAll();
  }
});

it’s a node library you need to use wrap async or similar

I tried

// 1-
    getSequelizeData() {
        let sequelize = new Sequelize('meteor', 'sa', '123', {
            host: '192.168.0.109',
            dialect: 'mssql',
        });

        let User = sequelize.define('user', {
            firstName: {
                type: Sequelize.STRING
            },
            lastName: {
                type: Sequelize.STRING
            }
        });

        return User.findAll();
    },
------------
// 2-
async getSequelizeData() {
        let sequelize = await new Sequelize('meteor', 'sa', '123', {
            host: '192.168.0.109',
            dialect: 'mssql',
        });

        let User = sequelize.define('user', {
            firstName: {
                type: Sequelize.STRING
            },
            lastName: {
                type: Sequelize.STRING
            }
        });

        return User.findAll();

It don’t work, show

Executing (default): SELECT [id], [firstName], [lastName], [createdAt], [updatedAt] FROM [users] AS [user];

Not necessarily. Meteor.wrapAsync is only good for asynchronous functions with a signature having a final callback(error, result) parameter.

However, most popular npm modules now come with Promise support built in. The sequelize package does provide a Promise api, which is what @theara is trying to use here.

OK, I’ve had a play with sequelize. The “problem” seems to be that “finders” returns a lot of overhead data in the returned object. From the doc:

… They do not return plain objects but instead return model instances…

And about instance valuess:

If you log an instance you will notice, that there is a lot of additional stuff. In order to hide such stuff and reduce it to the very interesting information, you can use the get-attribute. Calling it with the option plain = true will only return the values of an instance.

Having found all that I’ve still not got it working.

However, I have got raw queries working without issue, so you may want to try those.

Meteor.methods({
  async getData() {
    const all = await sequelize.query('SELECT [id], [firstName], [lastName], [createdAt], [updatedAt] FROM [users] AS [user]', { type: sequelize.QueryTypes.SELECT});
    return all;
  },
});

Very thanks for your reply.
It is not got about query string.
Now I tried to node-mssql, It work fine for:

Meteor.methods({
  async getData() {
    return await .........;
 }
});

but I have problem with installation Ms SQL docker on Mac.
(don’t work)

I can’t help with that.

For the sake of completeness and my own peace of mind, I got this working:

Meteor.methods({
  async getData() {
    const all = await User.findAll();
    return all.map(element => {
      return element.toJSON();
    });
  }
});

The issue being that findAll() returns an array of model instances, rather than a single model instance. That means the return data has to be “post processed” row-by-row (which is what’s happening in the map).

thanks again, I will try soon.
But now I don’t connect toMs SQL docker from Mac.
Oh oh…

Please advise, I should be try node-mssql or sequelize?

I would like to use async on mdg:validated-method`

export const usersInsert = new ValidatedMethod({
    name: 'getData',
    mixins: [CallPromiseMixin],
    validate: null,
    async run() {
            let connection = await new sql.connect("mssql://sa:123@192.168.0.109/meteor");
            let request = new sql.Request(connection);

            return request.query('select * from users');
        }

    },
});

It correct or not?

Now it is OK to use anys in Method