2015-09-27 174 views
1

在Angular中有一個應用程序。不明白如何正確保存數據。將數據正確保存到mongoDB中

這是我的API的一部分:

.post('/type/add', function(req, res){ 
    var type = new NewType({ 
     type: req.body.type, 
     subtype: req.body.subtype, 
    }); 
    type.save(function(err, newTypeOne){ 
     if(err){ 
     res.send(err); 
     return; 
     } 
     res.json({message: "New type Created!"}); 
    }); 
}) 

這是我的MongoDB的架構:

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var Type = new Schema({ 
    type: String, 
    subtype: String 
}); 
module.exports = mongoose.model('NewType', Type); 

這是我的控制器:

.controller('AddTypeController', ['CreateType', 'AllType', '$location', function(CreateType, AllType, $location){ 
    vm = this; 
    vm.createType = function(){ 
    vm.message = ''; 
    CreateType.create(vm.typeData) 
     .success(function(data){ 
     vm.typeData = ''; 
     vm.message = data.message; 
     $location.path('/type') 
     }); 
    }; 
    AllType.getAll().success(function(data){ 
     vm.types = data; 
    }); 
}]) 

這是我的服務:

angular.module('typeService', []) 
.factory('AllType', function($http){ 
    var typeFactory = {}; 
    typeFactory.getAll = function(){ 
     return $http.get('/api/type'); 
    }; 
    return typeFactory; 

}) 
.factory('CreateType', function($http){ 
    var typeFactory = {}; 
    typeFactory.create = function(typeData){ 
     return $http.post('/api/type/add', typeData); 
    }; 
    return typeFactory; 
}) 

我的HTML:

<div ng-controller="AddTypeController as type"> 
<form> 
    <md-select ng-model="type.typeData.type" placeholder="Type"> 
     <md-option ng-value="typeone.type" ng-repeat="typeone in type.types">{{typeone.type}}</md-option> 
    </md-select> 
    <md-input-container flex> 
     <label>SubTyp</label> 
     <input ng-model="type.typeData.subtype"> 
    </md-input-container> 
    <button ng-click="type.createType()">Send</button> 
</form> 
</div> 

現在我有一個問題: 例如我沒有數據呢。我加入第一個選擇 - TypeOne,並在第二個選擇 - SubTypeOne。在數據庫中現在我有{type:TypeOne,subtype:SubTypeOne}。當我將第二個子類型添加到TypeOne時,我首先選擇TypeOne並在輸入中添加子類型 - SubTypeTwo。現在我有數據庫中的第二條記錄{type:TypeOne,subtype:SubTypeTwo},並在此之後,在第一次選擇我有TypeOne兩次。

但我想要做這樣的事情:如果我已經有需要的類型,並且只想添加新的子類型。在這種情況下,我想在數據庫中像這樣:{type:TypeOne,subtype:[subtype:SubTypeOne,subtype:SubTypeTwo]}。爲此,我應該像存儲數組一樣保存子類型,但不知道如何。

回答

0

在你的模型中,你應該將子類型作爲一個字符串數組。然後你可以將它們推到該陣列上。一個例子如下:

var Type = new Schema({ 
    type: String, 
    subtype: [String] 
}); 

然後你就可以有一個更新方法(貓鼬:findOneAndUpdate),而不是隻建立一個新的類型。或者你可以讓它檢查它是否能找到一個,然後更新它,否則創建一個新的。示例如下所示:

CreateType.findOneAndUpdate({type: 'type1'}, 
    {$push: {subtype: 'type2'} }, 
    function(err, data) { 
    if (err) 
     // Do the create for a new one here 
    else 
     // Send back a 200 OK or whatever else 
}); 

這將允許您按原樣更新陣列。我鼓勵您查看關於貓鼬的findOneAndUpdate文檔以及$push mongo文檔。

希望這會有所幫助!我最近一直在研究一個應用程序,基本上正在做我在這裏描述的內容。所以我實際上一直在使用它來證明它的工作原理。

+0

謝謝。我正在做這樣的事情,但不起作用。總是有其他... Type.findOneAndUpdate({type:req.body.type},{$ push:{subtype:req.body.subtype}},function(err,type){if(err){ type.save(函數(ERR,NEWTYPE){ 如果(ERR){ res.send(ERR); 回報; } res.json({消息: 「創建」}); }); } 其他{ res.json({消息:req.body.type});} } ) –

+0

我不認爲你應該,如果你正在使用findOneAndUpdate需要type.save()(因爲我沒不需要它)。 – JMoser

相關問題