how to execute MongoDB statements in strongloop's remote method

Hi My task is to create an remote method using strongloop & execute an mongoDB function for the rest API. my mongoDB is like

"db.users.count({user_email_id :"bhargavb@ideaentity.com",user_password:"5f4dcc3b5aa765d61d8327deb882cf99",isdeleted:1,user_status:1}); if (count=1) then"

to execute this in strongloop I'm trying something like

module.exports = function(GetUserFind) {
    var server = require('../../server/server');
    var db = server.dataSources.MongoTours;
    db.connect(function(err, db) {
        if (err) return console.log('error opening db, err = ', err);
        console.log("db opened!");


        GetUserFind.login = function(par,par2,par3,par4,cb){
            console.log(par,par2,par3,par4);
            db.collection('users', function(err, collection) {
                console.log("the collection is"+ collection);
                if (err) return console.log('error opening users collection, err = ', err);
                collection.count ({user_email:par}, function(err, result) {
                    if(err) {
                        console.error(err);
                        return;
                    }
                    cb(null,result);
                });

                cb(null,par,par2,par3,par4);
            });
        }
    });

    GetUserFind.remoteMethod(
        'login',
        {
            accepts: [{arg: 'user_email_id', type: 'string'},
            {arg: 'user_password', type: 'string'},
            {arg: 'isdeleted', type: 'number'},
            {arg: 'user_status', type: 'number'}],
            returns: {arg: 'result', type: 'object'},
            http: {path:'/Loogin', verb: 'get'}
        }
    );
    //  db.close();

}

but I'm not able to do the same, can anyone tell me how can i execute mongoDB statements in strongloop remote methods

thanks in advance

Answers


I tried it the following way,

module.exports = function(GetUserFind) {

    var response = 'User does not exists';
    var server = require('../../server/server');

    var sha256 = require('js-sha256');
    // getUser function login declaration
    GetUserFind.login = function(par, par2, cb) {
        console.log(par, par2);
        // calling users collection method
        var MongoClient = require('mongodb').MongoClient;
        var self = this;
        this.callback = cb;
        // to connect to mongoDB datasource
        MongoClient.connect('mongodb://192.168.0.78:27017/tours', function(err,
                db) {

            console.log(sha256(par2));

            if (err) {
            // try{
                console.log(err);

            }


            db.collection('users').find({
                user_email_id : par,
                user_password : sha256(par2)
            },{user_password:0}).toArray(function(err, result) {
                //https://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/

                if (err) {
                    throw err;
                }
                if (result.length > 0) {
                self.callback(null, result);
                    // db.disconnect();
                } else {
                    self.callback(null, response);
                    // db.disconnect();
                }
            });

        });

    }

    GetUserFind.remoteMethod('login', {
        // passing input parameters
        accepts : [ {
            arg : 'user_email_id',
            type : 'string'
        }, {
            arg : 'user_password',
            type : 'string'
        } ],
        // getting output parameters
        // returns: {arg: 'user_email_id', type: 'string'},
        returns : [ {
            arg : 'result',
            type : 'object'
        } ],
        http : {
            path : '/Loogin',
            verb : 'get'
        }
    });
    // db.close();


};

This may help other.

If any one knows better & easy way, Please post or update the answer


Need Your Help

Ignore Hibernate @Where annotation

hibernate annotations where-clause

I have an Entity which has an association to another Entity annotated with @Where, like so

Rails has_many :through association

ruby-on-rails ruby ruby-on-rails-4 relationship activemodel

I'm trying to create a rails app where user can create events and invite participants to it and need your help!