1

I have a remote method for loopback that I am trying to implement.

"use strict";

module.exports = function(Quote) {
  /**
   *
   * @param {Function(Error, object)} callback
   */

  Quote.random = function(callback) {
    Quote.getDataSource().connector.connect(function(err, db) {
      var collection = db.collection('Quote');
      collection.aggregate([
        {Sample: {size: 1}},
      ], function(err, data) {
        if (err) return callback(err);

        return callback(null, data);
      });
    });
  };
};

But every time I try to view it in the loopback api explorer I am getting this error.

/Users/macuser/Documents/projects/loopback/Quotes/node_modules/mongodb/lib/utils.js:132
  throw err;
  ^
TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at stringify (/Users/macuser/Documents/projects/loopback/Quotes/node_modules/express/lib/response.js:1119:12)
at ServerResponse.json (/Users/macuser/Documents/projects/loopback/Quotes/node_modules/express/lib/response.js:260:14)
at Object.sendBodyJson [as sendBody] (/Users/macuser/Documents/projects/loopback/Quotes/node_modules/strong-remoting/lib/http-context.js:437:7)
at HttpContext.done (/Users/macuser/Documents/projects/loopback/Quotes/node_modules/strong-remoting/lib/http-context.js:578:24)
at /Users/macuser/Documents/projects/loopback/Quotes/node_modules/strong-remoting/lib/rest-adapter.js:539:11
at /Users/macuser/Documents/projects/loopback/Quotes/node_modules/async/dist/async.js:3888:9
at /Users/macuser/Documents/projects/loopback/Quotes/node_modules/async/dist/async.js:473:16
at replenish (/Users/macuser/Documents/projects/loopback/Quotes/node_modules/async/dist/async.js:1006:25)
at iterateeCallback (/Users/macuser/Documents/projects/loopback/Quotes/node_modules/async/dist/async.js:995:17)
at /Users/macuser/Documents/projects/loopback/Quotes/node_modules/async/dist/async.js:969:16
at /Users/macuser/Documents/projects/loopback/Quotes/node_modules/async/dist/async.js:3885:13
at interceptInvocationErrors (/Users/macuser/Documents/projects/loopback/Quotes/node_modules/strong-remoting/lib/remote-objects.js:724:22)
at /Users/macuser/Documents/projects/loopback/Quotes/node_modules/async/dist/async.js:473:16
at replenish (/Users/macuser/Documents/projects/loopback/Quotes/node_modules/async/dist/async.js:1006:25)
at iterateeCallback (/Users/macuser/Documents/projects/loopback/Quotes/node_modules/async/dist/async.js:995:17)
[nodemon] app crashed - waiting for file changes before starting...

Has something changed with mongodb?

4

2 Answers 2

1

According to the mongodb node driver docs the aggregate function now returns a cursor (from 2.6). Aggregation changed, now return a AggregationCursor.

Try this code and let me know the result. This worked for me.

'use strict';

module.exports = function(Quote) {
    /**
     *
     * @param {Function(Error, object)} callback
     */

    Quote.random = function(callback) {
        Quote.getDataSource().connector.connect( function (err, db)  {
            var collection = db.collection('Quote');
            var check = collection.aggregate([ {$sample: { size: 1}}]);
            check.get(function (err, data) {
                if(err) return callback(err);
                return callback(null, data);
            })
        });
    };
};

Ref link: https://github.com/strongloop/loopback-connector-mongodb/issues/434#issuecomment-396890775

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

Comments

0

Please check below how to create Remote Method in loopback. Reference Link : here

module.exports = function(Quote) {
  Quote.remoteMethod('random', {
            accepts: [{ //if required
              arg: 'filter',
              type: 'object',
              required: false
          }],
            http: {path: '/list', verb: 'get'},
          returns: [
              { type: 'array', root: true } //change as per requirement 
          ]
  });

  Quote.random(filter, callback) { 

    let quoteCollection = Quote.getDataSource().connector.collection("Quote");
    quoteCollection.aggregate([
          {Sample: {size: 1}},
        ], function(err, data) {
          if (err) return callback(err);

          return callback(null, data);
        });
    }
};

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.