TypeError:将循环结构转换为JSON环回和mongodb

问题描述 投票:1回答:2

我有一个远程方法用于我正在尝试实现的环回。

"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);
      });
    });
  };
};

但是每当我尝试在loopback api explorer中查看它时,我都会收到此错误。

/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...

有什么变化与mongodb

javascript mongodb loopbackjs
2个回答
0
投票

请查看下面如何在环回中创建Remote Method。参考链接: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);
        });
    }
};

0
投票

根据mongodb节点驱动程序文档,聚合函数现在返回一个游标(从2.6开始)。聚合已更改,现在返回AggregationCursor。

试试这段代码,让我知道结果。这对我有用。

'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);
            })
        });
    };
};

参考链接:https://github.com/strongloop/loopback-connector-mongodb/issues/434#issuecomment-396890775

© www.soinside.com 2019 - 2024. All rights reserved.