Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
为什么我的JSON不能正确地从客户端传输到服务器端_Json_Node.js_Mongodb - Fatal编程技术网

为什么我的JSON不能正确地从客户端传输到服务器端

为什么我的JSON不能正确地从客户端传输到服务器端,json,node.js,mongodb,Json,Node.js,Mongodb,我有以下JSON: { "fields":[ {"name":"thom","techname":"rgom","description":"dfgkjd","type":"text"}, {"name":"thom","techname":"rgom2","description":"dfgkjd","type":"text"} ] } 当我使用以下代码将其发布到NodeJS服务器时: $.ajax({ data : data,

我有以下JSON:

{
  "fields":[
    {"name":"thom","techname":"rgom","description":"dfgkjd","type":"text"},
    {"name":"thom","techname":"rgom2","description":"dfgkjd","type":"text"}
   ]
}
当我使用以下代码将其发布到NodeJS服务器时:

    $.ajax({ 
      data : data, 
      type: 'POST',
      dataType: 'json',
      timeout: 10000,         
      url : '/schema/create', 
      success : function(response) {
        console.log(response)
      },
      complete : function() {
      },
      error: function(x, t, m) {
        if(t==="timeout") {
          alert("Timeout");
        } else {
          alert("Der opstod følgende fejl:" + t + x + m + ". Kontakt COWI");
        }
      }

    });
并插入:

db.collection('schemas').insert(fields, {upsert:true}, function(err, result) {
  if(!err){
    console.log("written");
    console.log(result);

  }
});
在MongoDB,我有:

"_id" : ObjectId("5512ed12ecacf6e01da7aaa4"),
"fields[0][name]" : "thom",
"fields[0][techname]" : "rgom",
"fields[0][description]" : "dfgkjd",
"fields[0][type]" : "text",
"fields[1][name]" : "thom",
"fields[1][techname]" : "rgom2",
"fields[1][description]" : "dfgkjd",
"fields[1][type]" : "text"
我期待着:

{
  "_id":ObjectId("5512ed12ecacf6e01da7aaa4"),
  "fields":
  [
    {
      "name":"thom", 
      "techname" : "rgom",
      "description" : "dfgkjd",
      "type" : "text"
    },
    {
      "name":"thom", 
      "techname" : "rgom2",
      "description" : "dfgkjd",
      "type" : "text"
    }
  ]
}
编辑: 插入前记录:

[ { 'fields[0][name]': 'thom',
    'fields[0][techname]': 'rgom',
    'fields[0][description]': 'dfgkjd',
    'fields[0][type]': 'text',
    'fields[1][name]': 'thom',
    'fields[1][techname]': 'rgom2',
    'fields[1][description]': 'dfgkjd',
    'fields[1][type]': 'text',
    _id: 5512ed12ecacf6e01da7aaa4 } ]
编辑2 插入前记录的控制台(使用JSON.stringify()):


在db insert之前记录
字段
时获得的格式表示您将数据作为表单参数从客户端发送到服务器。如果这样做,您必须手动将其转换回一个对象以插入mongodb。如果您只是将其作为json字符串发送,那么就更容易了,这样您就不必在服务器端对其进行转换

data: JSON.stringify(data),
contentType: 'application/json'

两者看起来相当,只是记录方式不同。mongo控制台中的db.schemas.find().pretty()输出的“fields[n][field\u name]”类型在哪里?我认为它们看起来并不等效,因为“fields[1][description]”被引用,因此是一个名称。
console.log(fields)
在insert语句之前。另外,在客户端,
数据是什么?一个对象或字符串。在客户端数据是一个对象
data: JSON.stringify(data),
contentType: 'application/json'