2014-04-27 19 views
0

所以我有我的nodeJs服务器代码,这需要插入的东西到mongoDB这个方法,问题是,即使它插入(我看到它后面的数据库)它不会得到到成功的电话。成功的方法不会调用 - nodejs使用快递

我要的是让我的客户一个警告说“完成”

我正在通过快递使用的NodeJS。

这是我的服务器端:

exports.savePre = function(db) { 
    return function(req, res) { 

     // Get our form values. These rely on the "name" attributes 
     var json = req.body.jsonToSave; 
     var date = req.body.date; 
     var name = req.body.name; 
     var ArrayOfSlice = req.body.ArrayOfSlice; 


     // Set our collection 
     var collection = db.get('PresentationCollection'); 

     // Submit to the DB 
     collection.insert({ 
      "JsonToSave": json, 
      "Date": date, 
      "Name": name, 
      "ArrayOfPoints": ArrayOfSlice 
     }, function (err, inserted) 
     { 
      res.writeHead(200, { 'content-type': 'text/plain' }); 
      res.end(); 

      if (err) 
      { 
       // If it failed, return error 
       res.send("There was a problem adding the information to the database."); 
      } 
      else 
      { 
       res.send("bla"); 

       //res.writeHead(200, { 'content-type': 'text/plain' }); 
       //res.end(); 
       //res.setHeader("Access-Control-Allow-Origin", "*"); 
       // If it worked, set the header so the address bar doesn't still say /test 
       //res.end(); 
       // // And forward to success page 
       // res.redirect("userlist"); 
      } 
     }); 

    } 
} 

这是我在我的客户打电话:

function savePre() 
     { 
      var exporter = new THREE.SceneExporter(); 
      var sceneJInson = JSON.stringify(exporter.parse(scene)); 

      var CameraPosition = camera.position; 
      //var CameraLookAt = lookAtPosition; 

      // Array Of Slice, each row is a slice - here we will add all the slices :) 
      var arrayOfSlice = [{ cameraPosition: CameraPosition }]; 
      //     { cameraPosition: CameraPosition, CameraLookAt: CameraLookAt }, 
      //     { cameraPosition: CameraPosition, CameraLookAt: CameraLookAt }]; 

      // TODO : give the name where the user chose, this is only test, date is also a test 
      var parameters = { name: "eranNew", date: new Date(), jsonToSave: sceneJInson, ArrayOfSlice: JSON.stringify(arrayOfSlice) }; 

      $.ajax({ 
       url: '/savePre', 
       type: 'POST', 
       data: parameters, 
       success: function (result) { 
        alert("done"); 
       }, 
       error: function (result) { 
        alert("error"); 
       }, 
       dataType: 'json' 
      }); 
     } 

任何帮助将是篦。

回答

0

这是因为你写了几次标题。

collection.insert({ 
     "JsonToSave": json, 
     "Date": date, 
     "Name": name, 
     "ArrayOfPoints": ArrayOfSlice 
    }, function (err, inserted) 
    { 

     if (err) 
     { 
      res.send(500, "There was a problem adding the information to the database."); 
     } 
     else 
     { 
      res.send(200, "bla"); 

     } 
    }); 
+0

还是不行,它并获取到其他与“喇嘛”创建消息200,但在客户端也只能达到误差部分而不是成功的成功(我在我的调试中看到它) – eran10

+0

@ eran10如果你尝试没有jQuery(xhr)? – Vinz243

+0

我不明白你... – eran10

0

我能够以此来解决这个问题:

if (err) 
      { 
       res.send(500, "There was a problem adding the information to the database."); 
      } 
      else 
      { 
       res.statusCode = 200; 

       res.json("OK"); 
      }