TypeError:将循环结构转换为JSON! Api请求在快递js中失败

时间:2016-12-10 02:21:29

标签: json node.js express

我使用快速JS来编写API,而且我对此非常新!我已经能够编写我的代码用于登录,注销和注册等。但是当我尝试创建一个用于创建和订购的API时

  

TypeError:在Object.stringify(native)将循环结构转换为JSON
在stringify(/ Users / vaishakdinesh / Desktop / Web Design / webDesignProject / SousChef / SousChefWeb / node_modules / express / lib / response.js:1064:12)
在ServerResponse.json(/ Users / vaishakdinesh / Desktop / Web Design / webDesignProject / SousChef / SousChefWeb / node_modules / express / lib / response.js:243:14)
在ServerResponse.send(/ Users / vaishakdinesh / Desktop / Web Design / webDesignProject / SousChef / SousChefWeb / node_modules / express / lib / response.js:152:21)
在/ Users / vaishakdinesh / Desktop / Web Design /webDesignProject/SousChef/SousChefWeb/API/authenticate.js:20:13
在Layer.handle [as handle_request](/ Users / vaishakdinesh / Desktop / Web

这是我的快递档案中的护照代码。

passport.use('order', new LocalStrategy({
        passReqToCallback : true // allows us to pass back the entire request to the callback
    },
    function(req,done) {
        console.log("creating a new order");
        var newOrder = new Order();
        var obj = req.body.order;
        for(var i=0;i<obj.length;i++){
            newOrder.order[i].name = obj[i].name;
            newOrder.order[i].subtotal = obj[i].subtotal;
            newOrder.order[i].serving = obj[i].serving;
            newOrder.order[i].price = obj[i].price;
            newOrder.order[i].people = obj[i].people;
        }
        newOrder.charge.total = obj.charge.total;
        newOrder.charge.shipping = obj.charge.shipping;
        newOrder.user = obj.user;

        newOrder.save(function(err){
            if(err){
                console.log("saving error");
                return done("error in ordering",err);
            }
            console.log("order saved !");
            return(null,newOrder);
        });
    })
);

和发送的JSON对象看起来像:

     {
  "order":[
    {
      "name":"Butter chicken",
      "subtotal":"10",
      "serving":1,
      "people":1,
      "price":"10.00",
      "shortname":"Butter_Chicken"
    },
    {
      "name":"Butter chicken",
      "subtotal":"10",
      "serving":1,
      "people":1,
      "price":"10.00",
      "shortname":"Butter_Chicken"
    }
  ],
  "charge":[
    {
      "total":20,
      "shipping":0
    }
  ],
  "user":"v@gmail.com"
}

有谁知道为什么会出现这种错误?任何帮助,将不胜感激!提前谢谢!

编辑:

router.get('/success', function(req, res){
    req.session.cookie.lastLogin = new Date().getTime();
    req.session.name = req.user.name;
    req.session.username = req.user.email;
    req.session.password = req.user.password;
    res.send({status: 200,state: 'success', user: req.user ? req.user : null});
});

router.get('/mail/success', function(req, res){
    res.send({status: 200,state: 'failure', message:req ? req : null});
});

router.post('/order', passport.authenticate('order', { 
    successRedirect: '/auth/success', 
    failureRedirect: '/auth/failure' 
})); 

0 个答案:

没有答案