我正在我的应用程序(html 5 sdk)中构建一个食品订购系统。创建项目时,一个项目可以有多种配料(额外的奶酪、额外的鸡肉等)。
这就是我想做的
- 将项目插入
Items
表(我可以简单地做到这一点) - 获取从应用程序发送的配料数组并将其插入
配料
表中(不确定如何发送配料数组。每个配料都有名称和价格)我可以将它们添加为项目属性插入服务请求并发送?
问题
我可以毫无问题地插入一个项目,但我不知道如何获取配料数组列表并将其插入配料表中。
我可以将对象数组作为 Azure 移动服务服务请求中的属性发送吗?
感谢您提前抽出时间。
最佳答案
解决这个问题的一种方法是在 Items 表上使用自定义插入脚本,以便它解析数组并将数组的每个项目插入到 Toppings 表中(包括项目 ID,如果需要的话)。例如:
function insert(item, user, request) {
var toppings = item.Toppings;
item.Toppings = null;
request.execute({ success: function() {
var toppingsTable = tables.getTable('Toppings');
var count = 0;
toppings.forEach(function(topping, index) {
topping.itemId = item.id;
toppingsTable.insert(topping, {
success: function() {
count++;
if (toppings.length === count) {
request.respond();
}
},
error: function(err) {
console.warn('Error while inserting toppings objects', err);
count++;
if (toppings.length === count) {
request.respond();
}
});
}});
}
关于sql-server - Azure移动服务: Insert to multiple tables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20367057/