2012-10-08 36 views
0

通過ajax請求我已經獲取數據表單客戶端,並通過保存查詢將它保存在mongodb數據庫(mongoose)中。現在,我想知道如何查找數據並顯示它新一頁。從mongodb數據庫中查找數據並將其顯示在新的html頁面

我已經從客戶端獲取數據並將其保存在數據庫中。現在我希望當回調函數調用它時,從數據庫中查找數據並使用response.redirect將其顯示在新頁面中。請引導我。

$("#saveChanges5").live('click',function(){ 
      var sendingObj = { 
       regOperation: $("#area_operation").val() 
       ,regFieldAct: $("#field_activity").val() 
       ,regOther:  $("#other_details").val() 
      }; 

      $.ajax({ 
       url:'/WorkDetails' 
       ,type:'post' 
       ,data: sendingObj 
       ,success:function(){ 
        alert('Successfully saved') 
       }, 
       error: function(){ 
        alert("Saving Failed") 
       } 
      }) 

     }); 

app.post("/WorkDetails",function(req ,res){ 
console.log(req.body); 
saveWorkDetails(req.body.regOperation ,req.body.regFieldAct ,req.body.regOther ,function(){ 
    res.send("");//here i want to add new code 

}); 

});

function saveWorkDetails(area_operation ,field_activity ,other_details , callback){ 
console.log("save CALLED"); 
var receivedObj = new WorkDetailInfo({ 
    area_operation:area_operation , 
    field_activity:field_activity , 
    other_details:other_details 
}); 
console.log(receivedObj); 
receivedObj.save(function(err){ 
    console.log("inside Save "); 
    if(err){ 
     //res.send(err); 
     console.log(err); 
    } 
    else{ 
     callback(); 
    } 
}); 

}

+0

現在我有提到我的代碼 –

回答

0

您可以使用narwhal-mongodb的API 以下是使用例子:

var MongoDB = require("mongodb"); 
var db = new MongoDB.Mongo().getDB("mydb"); 

var colls = db.getCollectionNames(); 
colls.forEach(function(el) { print(el); }); 

var coll = db.getCollection("testCollection"); 
相關問題