0
我最近了解了NodeJS異步/等待。更改我的代碼,使其更清晰,更易於閱讀。TypeError:更改爲NodeJS時,將圓形結構轉換爲JSON異步等待
- 升級到NodeJS 8.2.1。
- MongoDB的2.2.30
- 快遞4.1.4
代碼如下所示:
var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var url = process.env.mongo;
/* GET home page. */
router.get('/', async function(req, res, next) {
try{
let db = await MongoClient.connect(url);
let col = await db.collection('product');
let documents = await col.find();
return res.send(documents)
}catch(err){
console.log(err)
}
});
module.exports = router;
這個錯誤有問題,
TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at stringify (/home/edmondtm/test-async/node_modules/express/lib/response.js:1064:12)
at ServerResponse.json (/home/edmondtm/test-async/node_modules/express/lib/response.js:243:14)
at ServerResponse.send (/home/edmondtm/test-async/node_modules/express/lib/response.js:152:21)
at /home/edmondtm/test-async/routes/index.js:15:16
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
注:沒有問題,如果更改: let documents =等待col.find();到 let documents = await col.findOne();
希望有人能幫助我。