2015-09-27 80 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

相关问题