Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.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
Javascript mongoose为每个ajax调用插入文档_Javascript_Node.js_Ajax_Mongodb_Mongoose - Fatal编程技术网

Javascript mongoose为每个ajax调用插入文档

Javascript mongoose为每个ajax调用插入文档,javascript,node.js,ajax,mongodb,mongoose,Javascript,Node.js,Ajax,Mongodb,Mongoose,我试图使用Mongoose将数据插入MongoDB,我创建了一个表单并使用两个ajax post将数据发送到节点,但是Mongoose为每个ajax调用插入两个文档,我希望将数据作为单个文档发送到数据 这是我的服务器: app.post("/cp" , upload , function(req , res){ console.log('file uploaded succcessfully'); var title = JS

我试图使用Mongoose将数据插入MongoDB,我创建了一个表单并使用两个ajax post将数据发送到节点,但是Mongoose为每个ajax调用插入两个文档,我希望将数据作为单个文档发送到数据

这是我的服务器:

app.post("/cp" , upload  , function(req , res){
            console.log('file uploaded succcessfully');
            var title = JSON.stringify(req.body.titles);
            var file = req.file;
 
            const courses = new Courses({ 
                // courseTitle:c_title,
                // courseSubtitle:c_subtitle,
                // courseAuthor : c_creator,
                // coursePrice : c_price,
                courseVideo :file ,
                courseTitles :title ,
                // courseSpecs : c_specs,
                courseValidation : 0
                });
                courses.save();
});
Mongoose插入带标题但不带文件的文档以及带文件但不带标题的文档

阿贾克斯:


在这种情况下,您应该将findOneAndUpdate方法与选项{upsert:true}:

一起使用。在这种情况下,您应该将findOneAndUpdate方法与选项{upsert:true}:

 if(e.submitter.id == "submitpostCp"){
          
         var data = {};
         data.titles = titlesLis;
         data.specs = specsLis;
         data.submit = "submitAll";
         var fileup =  new FormData($('#form')[0]);

      $.when(
         $.ajax({
            type: 'post',
            url: '/cp',
            data: JSON.stringify(data),
            contentType: 'application/json',
               xhrFields: {
               withCredentials: false
            },
            headers: {

            },
            success: function (data) {
               console.log('Success');
               console.log(data);

            },
            error: function () {
               console.log('We are sorry but our servers are having an issue right now');
            }

         })
         
       ).then(function() {
   
         $.ajax({
            url:'/cp',
            type: 'POST',
            contentType: false,
            processData: false,
            cache: false,
            data: fileup,
            success: function(res){
               //  alert(res);
 
            },
            error: function(){
                alert('Error: In sending the request!');
            }
        })    
       });
      }