TypeError:在将数据从服务器传输到html页面时将node结构中的循环结构转换为JSON?

时间:2015-09-03 07:04:05

标签: json node.js mongodb

我正在尝试在服务器上运行一个程序,将数据插入mongodb并进行检索。

当我尝试插入插入的数据但服务器出现故障时。

我的服务器代码是

server.js

var express = require("express"),
    app = express(),
    bodyParser = require('body-parser'),
    errorHandler = require('errorhandler'),
    methodOverride = require('method-override'),
    hostname = process.env.HOSTNAME || 'localhost',
    port = parseInt(process.env.PORT, 10) || 4004,
    publicDir = process.argv[2] || __dirname + '/public';
var exec = require('child_process').exec;
var fs = require('fs');
var MongoClient = require('mongodb').MongoClient
    , format = require('util').format;



//Show homepage
app.get("/", function (req, res) {
  res.redirect("/index.html");
  console.log("shubham ");
});
app.get("/index/", function (req, res) {
  res.redirect("/index.html");
  console.log("shubham ");
});

app.get("/search", function (req, res){
  console.log("shubham batra");
   var pro_name = req.query.name;
   var pro_code = req.query.code;
   var pro_category = req.query.category;
   var pro_brand = req.query.brand;




  MongoClient.connect('mongodb://127.0.0.1:27017/prisync', function(err, db) {
  if (err) throw err;
    console.log("Connected to Database");


    var documen = {name:pro_name, code:pro_code , category:pro_category, brand:pro_brand };

  //insert record
  db.collection('urlinfo').insert(documen, function(err, records) {
    if (err) throw err;

    });
  db.collection('urlinfo').find({} , function(err , products){   // LINE 48  ==========
  if(err){
    console.log(err);
    res.json(err);
  }else{
    res.json(products);   // LINE 53 ====================
  }
 });





});


});
//Search page
app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(express.static(publicDir));
app.use(errorHandler({
  dumpExceptions: true,
  showStack: true
}));

console.log("Server showing %s listening at http://%s:%s", publicDir, hostname, port);
app.listen(port);

并提供以下错误:

/home/shubham/node_modules/mongodb/lib/utils.js:97
    process.nextTick(function() { throw err; });
                                        ^
TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at ServerResponse.json (/home/shubham/Music/pricesync/server/node_modules/express/lib/response.js:242:19)
    at /home/shubham/Music/pricesync/server/server.js:53:9
    at handleCallback (/home/shubham/node_modules/mongodb/lib/utils.js:95:12)
    at Collection.find (/home/shubham/node_modules/mongodb/lib/collection.js:339:44)
    at /home/shubham/Music/pricesync/server/server.js:48:28
    at /home/shubham/node_modules/mongodb/lib/mongo_client.js:439:11
    at process._tickCallback (node.js:448:13)

0 个答案:

没有答案