2016-06-07 99 views
0

我想保存從表單(html)中獲取的數據。 我的表單中有相同的文件,我想將它們從html頁面發送到node.js頁面,然後從那裏發送到mongoDB(mlab)數據庫。 在我的節點頁面中,我使用角度來保存我獲得的用戶數據。 我正在使用ng-controller連接到js頁面,並使用ng-model來保存我在輸入標籤中獲得的數據。 我的問題是,我不能保存數據在mongoDB?保存表單中的數據

感謝,

HTML:

<body ng-controller="addController"> 
     <h1 class="text-center">Add Activity</h1> 
     <form class="form-group" ng-submit="createActivity()"> 
      <div class="form-group"> 
      <div class="col-sm-10"> 
      <label for="inputEmail3" class="col-sm-2 control-label">Title:</label> 
      <input class="form-control" type="text" placeholder="Title" ng-model="title"></input> 
      </div> 
      <div class="col-sm-10"> 
      <label for="inputEmail3" class="col-sm-2 control-label">Age:</label> 
      <input class="form-control" id="inputEmail3" type="number" placeholder="Age" ng-model="age" min="0" max="16"></input> 
      </div> 
      <div class="col-sm-10"> 
      <label for="inputEmail3" class="col-sm-2 control-label">Description:</label> 
      <textarea class="form-control" id="inputEmail3" type="text" placeholder="Description" ng-model="description"></textarea> 
      <br> 
      </div> 
      <div class="col-sm-10"> 
      <label for="inputEmail3" class="col-sm-2 control-label">Themes:</label> 
       <input type="radio" name="themes" value="frozen" ng-model="theme">frozen &nbsp; 
       <input type="radio" name="themes" value="minions" ng-model="theme">minions &nbsp; 
       <input type="radio" name="themes" value="heroes" ng-model="theme">heroes 
       <input type="radio" name="themes" value="princess" ng-model="theme">princess &nbsp; 
       <input type="radio" name="themes" value="piraets" ng-model="theme">piraets &nbsp; 
       <input type="radio" name="themes" value="none" ng-model="theme">none 
       <br> 
      </div> 
      <div class="col-sm-10"> 
      <br> 
      <input type="file" name="upload" ng-model="image"></input> 
      </div> 
      <div class="col-sm-10"> 
      <br> 
      <input type="submit" class="btn btn-default" name="send"></input> 
      </div> 
      </div> 
     </form> 

JS:

var addActivityApp = angular.module('addActivityApp',['$scope','$resource']); 
var Activity = $resource('/activities'); 
var activity = new Activity(); 

$scope.createActivity=function() { 
    var activity = new Activity(); 
    activity.title = $scope.title; 
    activity.age = $scope.age; 
    activity.description = $scope.description; 
    activity.theme = $scope.theme; 
    activity.$save(function (result) { 
     $scope.activities.push(result); 
     $scope.title = ''; 
     $scope.age = ''; 
     $scope.description = ''; 
     $scope.theme = ''; 
    }); 
    }); 



addActivityApp.controller('addController',function($scope) { 
    $scope.addController = model; 


}); 
+0

哪裏是你到MongoDB的連接?我的連接是來自服務器的 –

+0

這是我的客戶說的。 – adi

回答

0

你可以試試下面的代碼。檢查你的服務器端是否使用下面的代碼。

app.post('yourserverurl', function(req, res) { 
    console.log(req.body) //Here all form data will display 
    var InsertData = req.body; 

    db.collection('restaurants').insertOne(InsertData,function(err, result) { 
    assert.equal(err, null); 
    console.log("Inserted a document into the collection."); 
    callback(); 
    }); 


}); 

,如果你分享,你已經寫在服務器端的整個代碼,這樣我就可以把它簡化更