MongoDB nodeJS - 转换圆形结构

mongodb nodejs - converting circular structure

本文关键字:结构 转换 nodeJS MongoDB      更新时间:2024-05-30

我有一些代码可以从集合中提取所有文档并将其放到网页上。 简化版本如下所示:

var mongodb = require("mongodb"),
    express = require("express"),
    mongoServer = new mongodb.Server('localhost', 27017),
    dbConnector = new mongodb.Db('systemMonitor', mongoServer),
    db;
var app = new express();
app.get('/drives', function(req, res) {
  db.collection('driveInfo', function(err, collection) {
    if (err) throw err;
    collection.find({}, function(err, documents) {
      res.send(documents);
    });
  });
});
dbConnector.open(function(err, opendb) {
  if (err) throw err;
  db = opendb;
  app.listen(80);
});

我有一个 driveInfo 集合,其中包含一长串文档。每个文档都包含嵌套对象。我想做的是,每当有人在浏览器中访问/drives,将整个集合打印为 json 对象,以便我以后可以使用 jquery 获取所有内容(API 的开头(

但是,我收到一个错误,说"类型错误:将循环结构转换为 JSON"。页面上的错误指向以下代码行:

collection.find({}, function(err, documents) {
  res.send(documents);
});

我不确定问题是什么,或者自我参考在哪里。我是否没有正确查询集合?

不确定您使用的是哪个版本的 API,但我认为您的语法在查看 API 规范时可能是错误的:

http://docs.mongodb.org/manual/reference/method/db.collection.find/

这是声明:

db.collection.find(<criteria>, <projection>)

而且您肯定滥用了投影参数。像您所做的那样传递回调似乎在结果中返回 db 对象,这会导致在 express 中的 JSON 序列化期间出现循环错误。

查找所有操作的正确代码应如下所示:

collection.find({}).toArray(function(error, documents) {
    if (err) throw error;
    res.send(documents);
});

就我而言,我收到错误,因为我正在查询(使用猫鼬查找方法(而没有进行等待。请看下文

给出错误的查询(因为我尚未使用 await 执行此查询(:

const tours = Tour.find({
    startLocation: {
      $geoWithin: { $centerSphere: [[longitude, latitude], radius] }
    }
  });

由于这个原因,我上了邮递员的错误:

"message": "Converting circular structure to JSON'n    --> starting at object with constructor 'NativeTopology''n    |     property 's' -> object with constructor 'Object''n    |     property 'sessionPool' -> object with constructor 'ServerSessionPool''n    --- property 'topology' closes the circle"

我如何摆脱上述错误(添加等待(:

 const tours = await Tour.find({
        startLocation: {
          $geoWithin: { $centerSphere: [[longitude, latitude], radius] }
        }
      });

回调选项来自Mongoose而不是MongoDB请参阅文档。

// Mongoose Docs : callback option
MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});

// Example
app.get( '/api/users' , (req,res,done)=>{
  let getUsers = NewUser.find({},(err,data)=>{
    if(err) return done(err);
    res.json(data)
  });
});

看看响应是回调,在你的情况下它会是

YourModel.find({}, function(err, documents) {
  if(err) return done(err);
  res.send(documents);  //  <-- here
});
// <-- not here

在Mongo中,有一个光标方法来访问文档next()查看文档:

var myCursor = db.bios.find( );
var myDocument = myCursor.hasNext() ? myCursor.next() : null;
if (myDocument) {
    var myName = myDocument.name;
    print (tojson(myName));
}
<小时 />

您可以在 manual/crud 的 mongo 文档中找到 CRUD 操作。在"查询文档"中,您将看到db.inventory.find( {} ):若要选择集合中的所有文档,请将空文档作为查询筛选器参数传递给 find 方法。

<小时 />

异步/等待函数解决方案:Mongo 文档

app.get( '/api/users' , async (req,res)=>{
  const getUsers = await NewUser.find({});
  res.json( getUsers );
})

<回调>解决方案:猫鼬文档。

app.get( '/api/users' , (req,res,done)=>{
  let getUsers = NewUser.find({},(err,data)=>{
    if(err) return done(err);
    res.json(data)
  });
});
<小时 />

const res1 = await db.collection("some-db").find()

在这里,res1 将包含一个具有圆形结构的"光标",因此抛出给定的错误。

尝试向代码添加const res2 = await res1.toArray()

在这里,res2现在将包含一个文档数组,由光标 res1 指向,这是您正在查询的文档。