Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 为什么我要传递{';things[]';:[';apple';,';抱枕';]}表单数据?_Angularjs_Node.js_Rest_Mongoose - Fatal编程技术网

Angularjs 为什么我要传递{';things[]';:[';apple';,';抱枕';]}表单数据?

Angularjs 为什么我要传递{';things[]';:[';apple';,';抱枕';]}表单数据?,angularjs,node.js,rest,mongoose,Angularjs,Node.js,Rest,Mongoose,我在node.js/mongoose中有一个非常简单的模式,如下所示: var thingSchema = mongoose.Schema({ thing_name : String, ting_logo : String, things : [String] }); > db.things.find({}) { "_id" : ObjectId("5646bb2e4235cd620080a969"), "thing_name" : "test001",

我在node.js/mongoose中有一个非常简单的模式,如下所示:

var thingSchema = mongoose.Schema({
    thing_name  :  String,
    ting_logo  :  String,
    things : [String]
});
> db.things.find({})
{ "_id" : ObjectId("5646bb2e4235cd620080a969"), "thing_name" : "test001", "thing_logo" : "testlogo", "things" : [ "[\"Skiing\",\"Mountain Biking\"]" ], "__v" : 0 }
> 
这是我在angular.js控制器中的add方法:

    $scope.submit_thing  = function() {
        var data = $.param({ 'thing_name': $scope.newthing, 'thing_logo': $scope.newlogo, 'things': $scope.selected.things });
        $http.post("http://10.0.0.2:8080/thing/add",
            data,
            {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(function(response) {
            console.log(response);
            console.log(response.data);
        });
        ...    
。。。但是当我调用
$scope.submit\u thing
我的node.js应用程序输出这个

{ thing_name: 'eeeee',
  thing_logo: 'eeeee',
  'things[]': [ 'Fishing', 'Mountain Biking' ] }
。。。然后我可以在我的数据库中找到这个记录,但是
things
数组是空的。
[]
到底是从哪里来的?我想这会弄乱我的表格数据。我的表单数据不应该被删除吗

{ thing_name: 'eeeee',
  thing_logo: 'eeeee',
  'things': [ 'Fishing', 'Mountain Biking' ] }
更新:我已更改行

        var data = $.param({ 'thing_name': $scope.newthing, 'thing_logo': $scope.newlogo, 'things': $scope.selected.things });
。。。到

        var data = { 'thing_name': $scope.newthing, 'thing_logo': $scope.newlogo, 'things': $scope.selected.things };
。。。但这似乎让事情变得更糟。现在我从node.js控制台获得了这个输出

{ '{"thing_name":"thing","thing_logo":"thing","things":["Running","Fishing"]}': '' }
**更新2**:

好的,我现在在thingCtrl.js文件中执行此操作:

    $scope.submit_thing  = function() {
        var data = $.param({ "thing_name": $scope.newthing, "thing_logo": $scope.newlogo, "things": JSON.stringify($scope.selected.things)});
。。。因此,我知道a完整记录正在进入我的mongodb,但mongo记录如下所示:

var thingSchema = mongoose.Schema({
    thing_name  :  String,
    ting_logo  :  String,
    things : [String]
});
> db.things.find({})
{ "_id" : ObjectId("5646bb2e4235cd620080a969"), "thing_name" : "test001", "thing_logo" : "testlogo", "things" : [ "[\"Skiing\",\"Mountain Biking\"]" ], "__v" : 0 }
> 
数组
things
中的字符串有引号转义是否重要?
这是怎么回事?

好的,我要做的就是

  $scope.submit_thing  = function() {
        var data = Object({ 'thing_name': $scope.newthing, 'thing_logo': $scope.newlogo, 'things': $scope.selected.things});
        $http.post("http://10.0.0.2:8080/vendor/add",
            JSON.stringify(data)
            //{headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
         ).then(function(response) {
            console.log(response);
            console.log(response.data);
        });

我想我所做的就是注释掉标题行。

好的,下面是我要做的

  $scope.submit_thing  = function() {
        var data = Object({ 'thing_name': $scope.newthing, 'thing_logo': $scope.newlogo, 'things': $scope.selected.things});
        $http.post("http://10.0.0.2:8080/vendor/add",
            JSON.stringify(data)
            //{headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
         ).then(function(response) {
            console.log(response);
            console.log(response.data);
        });

我认为所做的事情是注释掉标题行。

您是否尝试过var data={thing\u name:$scope.newthing,thing\u logo:$scope.newlogo,things:$scope.selected.things}?我认为问题是uu传递到服务器对象,在那里事情是真的。你应该像这样通过smth:{name:'name',logo:'logo',things:['Fishing']}。你能试一下吗?你试过var data={thing\u name:$scope.newthing,thing\u logo:$scope.newlogo,things:$scope.selected.things}吗?我想问题是uu传递给服务器对象,在那里事情是真的。你应该像这样通过smth:{name:'name',logo:'logo',things:['Fishing']}。你能试试吗?