我一直在谷歌这几周没有真正的解决方案。
我相信有人会将此标记为副本,但我不确定它是否真的存在,也许我只是过于具体,无论如何都是这样。
我在我正在构建的node-webkit应用程序中使用angular。我有一个api内置表达式,我使用MongoDB(@mongolab
)和Mongoose作为数据库。
只要所有数据类型都是简单的字符串和数字,我就能正常工作。但我不得不重组数据以使用数组和复杂对象。重组数据后,我能够获得API调用后工作正常,但我无法让PUT
次调用工作。
数据如下所示:
itemRoles是一个数组,但我认为它正在抛出我现在得到的错误,所以我将它转换回字符串。
itemStats导致问题。 Angular正在寻找一个对象,但是itemStats是一个数组(我想无论如何)。 itemStats也曾经是一个字符串,但是如果它是一个具有key:value
对的对象数组,它在我的视图中更容易使用,这就是我改变它的原因。
我应该注意到我是MongoDB
的新手,第一次使用它。
{
"_id": {
"$oid": "55a10b9c7bb9ac5832d88bd8"
},
"itemRoles": "healer,dps",
"itemRating": 192,
"itemName": "Advanced Resolve Armoring 37",
"itemClass": "consular",
"itemLevel": 69,
"itemStats": [
{
"name": "Endurance",
"value": 104,
"_id": {
"$oid": "55a10b9c7bb9ac5832d88bda"
}
},
{
"name": "Willpower",
"value": 124,
"_id": {
"$oid": "55a10b9c7bb9ac5832d88bd9"
}
}
],
"__v": 0
}
Mongoose Schema看起来像这样:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//var stats = new Schema({
//name: String,
//value: Number
//});
var armoringSchema = new Schema({
itemType: String,
itemClass: String,
itemRoles: String,
itemLevel: Number,
itemName: String,
itemRating: Number,
itemStats: [{ name:String, value:Number}]
});
module.exports = mongoose.model('Armor', armoringSchema);
Express API路线:
/ on routes that end in /armors/:id
// ----------------------------------------------------
router.route('/armors/:id')
// get method omitted
// update the armoring with specified id (accessed at PUT http://localhost:8080/api/armors/:id)
.put(function(req, res) {
// use our armor model to find the armor we want
Armoring.findById({_id: req.params.id}, function(err, armor) {
if (err) {
return res.send(err);
}
for(prop in req.body) {
armor[prop] = req.body[prop];
}
// save the armor
armor.save(function(err) {
if (err) {
return res.send(err);
}
res.json({success:true, message: 'Armor updated!' });
});
});
})
资源工厂:
swtorGear.factory('armoringFactory', ['$resource', function ($resource) {
return $resource('http://localhost:8080/api/armors/:id', {}, {
update: { method: 'PUT', params: {id: '@_id'}},
delete: { method: 'DELETE', headers: {'Content-type': 'application/json'}, params: {id: '@_id'}}
});
}]);
编辑路线:
.when('/edit/armor/id/:id', {
templateUrl: 'views/modelViews/newArmor.html',
controller: 'editArmorCtrl',
resolve: {
armoring: ['$route', 'armoringFactory', function($route, armoringFactory){
return armoringFactory.get({ id: $route.current.params.id}).$promise;
}]
}
})
控制器(只是保存方法,控制器的第一部分用现有数据填充表单):
$scope.save = function(id) {
$scope.armor.itemStats = [
$scope.armor.stats1,
$scope.armor.stats2
];
$scope.armor.itemRoles = '';
if($scope.armor.role.tank) {
$scope.armor.itemRoles += 'tank';
}
if($scope.armor.role.healer) {
if($scope.armor.itemRoles != '') {
$scope.armor.itemRoles += ',healer';
} else {
$scope.armor.itemRoles += 'healer';
}
}
if($scope.armor.role.dps) {
if($scope.armor.itemRoles != '') {
$scope.armor.itemRoles += ',dps';
} else {
$scope.armor.itemRoles += 'dps';
}
}
console.log($scope.armor);
$scope.armor.$update(id)
.then(function(resp) {
if(resp.success) {
var message = resp.message;
Flash.create('success', message, 'item-success');
$scope.armors = armoringFactory.query();
} else {
var message = resp.message;
Flash.create('success', message, 'item-success');
}
});
}
通过PUT方法(来自console.log($scope.armor)
)发送的格式化数据:
保存时出错:
答案 0 :(得分:0)
我还没有以你正在做的方式看到嵌套模式。这里有一些尝试(很难说这是否肯定,还有很多事情发生):
var armoringSchema = new Schema({
itemType: String,
itemClass: String,
itemRoles: String,
itemLevel: Number,
itemName: String,
itemRating: Number,
itemStats: [{
name: String,
value: Number
}]
});
我们还需要将一个对象传递给$update
而不是一个数字。将$scope.armor.$update(id)
更改为$scope.armor.$update({id: id})
。