Posting object array to sails causes 'TypeError: Cannot convert object to primitive value'

此生再无相见时 提交于 2020-01-02 05:39:08

问题


In my html page i am sending this post to my sails server but I cannot get to the data in my controller as the req.param() function does not return any meaningful answer.

Here is the web page code

$.post("http://myserver.local/calendar/batch",
            {"batch":[
                {
                    "start_date" : "2014-06-27T09:00:00Z",
                    "title" : "abc",
                    "attendees" : [
                        "Fred Bloggs",
                        "Jimmy Jones",
                        "The Queen"
                    ],
                    "event_id" : "123",
                    "location" : "Horsham, United Kingdom",
                    "end_date" : "2014-06-27T10:30:00Z"
                },
                {
                    "start_date" : "2014-06-27T09:00:00Z",
                    "title" : "another",
                    "attendees" : [
                        "Fred Bloggs",
                        "Jimmy Jones",
                        "The Queen"
                    ],
                    "event_id" : "def",
                    "location" : "Horsham, United Kingdom",
                    "end_date" : "2014-06-27T10:30:00Z"
                }
            ]},
    function (data) {
        console.log("success");
    }
    ).fail(function(res){
        console.log("Error: " + res.getResponseHeader("error"));
    });

Here is the controller

module.exports = {
  batch: function (req, res, next){
    console.log("batch called");
    console.log("req.body" + req.body);
    console.log("req.param()" + req.param());
    res.send("ok");
  },

  _config: {}
};

I have tried using req.body but that does not seem to contain any content. This is what i get in the output

batch called
req.body=[object Object]
TypeError: Cannot convert object to primitive value
    at Array.toString (native)

回答1:


module.exports = {
 batch: function (req, res, next){
  console.log("batch called");
  console.log(req.body);
  console.log(req.param("batch"));
  res.send("ok");
},

_config: {}
};

If you use console.log("foo" + object) nodejs will try to convert the obeject into a string. Simply do console.log(object)



来源:https://stackoverflow.com/questions/24339780/posting-object-array-to-sails-causes-typeerror-cannot-convert-object-to-primit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!