将MongoDB结果字符串化为NodeJ的http响应

时间:2014-10-30 21:16:35

标签: javascript json node.js mongodb

我想向MongoHQ的MongoDB发送一个现有id的查询。此查询的结果需要与响应一起发送给用户。如果我记录此查询的结果,我会得到json响应。 现在,如果我将相同的结果字符串化,则会出现以下错误:

  

TypeError:将循环结构转换为JSON

代码:

http.createServer(function(request, response) {
    var tweety = db.find({"tweetId":"1337"});
    console.log(tweety);
    response.end(JSON.stringify(tweety));
}).listen(3000);

我从console.log()得到的JSON:

{ _mongooseOptions: {},
  mongooseCollection:
   { collection:
      { db: [Object],
        collectionName: 'posts',
        internalHint: null,
        opts: {},
        slaveOk: false,
        serializeFunctions: false,
        raw: false,
        pkFactory: [Object],
        serverCapabilities: undefined },
     opts: { bufferCommands: true, capped: false },
     name: 'posts',
     conn:
      { base: [Object],
        collections: [Object],
        models: [Object],
        replica: false,
        hosts: null,
        host: 'dogen.mongohq.com',
        port: 10004,
        user: 'x',
        pass: 'x',
        name: 'TwitterBitcoin',
        options: [Object],
        otherDbs: [],
        _readyState: 1,
        _closeCalled: false,
        _hasOpened: true,
        _listening: true,
        _events: {},
        db: [Object] },
     queue: [],
     buffer: false },
  model:
   { [Function: model]
     base:
      { connections: [Object],
        plugins: [],
        models: [Object],
        modelSchemas: [Object],
        options: [Object] },
     modelName: 'Post',
     model: [Function: model],
     db:
      { base: [Object],
        collections: [Object],
        models: [Object],
        replica: false,
        hosts: null,
        host: 'dogen.mongohq.com',
        port: 10004,
        user: 'x',
        pass: 'x',
        name: 'TwitterBitcoin',
        options: [Object],
        otherDbs: [],
        _readyState: 1,
        _closeCalled: false,
        _hasOpened: true,
        _listening: true,
        _events: {},
        db: [Object] },
     discriminators: undefined,
     schema:
      { paths: [Object],
        subpaths: {},
        virtuals: [Object],
        nested: {},
        inherits: {},
        callQueue: [],
        _indexes: [],
        methods: {},
        statics: {},
        tree: [Object],
        _requiredpaths: [],
        discriminatorMapping: undefined,
        _indexedpaths: undefined,
        options: [Object],
        _events: {} },
     options: undefined,
     collection:
      { collection: [Object],
        opts: [Object],
        name: 'posts',
        conn: [Object],
        queue: [],
        buffer: false } },
  op: 'find',
  options: {},
  _conditions: { tweetId: '527920990878433280' },
  _fields: undefined,
  _update: undefined,
  _path: undefined,
  _distinct: undefined,
  _collection:
   { collection:
      { collection: [Object],
        opts: [Object],
        name: 'posts',
        conn: [Object],
        queue: [],
        buffer: false } },
  _castError: null }

如何将JSON结果作为回复发送?

0 个答案:

没有答案