【问题标题】:Azure Mobile Service : Insert to multiple tablesAzure 移动服务:插入到多个表
【发布时间】:2013-12-04 04:47:12
【问题描述】:

我正在我的应用程序 (html 5 sdk) 中构建一个食品订购系统。创建项目时,一个项目可以有多个浇头(额外的奶酪、额外的鸡肉等)。

这就是我想做的事

  1. 将项目插入Items 表(我可以简单地做到这一点)
  2. 获取从应用程序发送的配料数组并将其插入Toppings 表(不确定如何发送配料数组。每个配料都有名称和价格)我可以将它们作为属性添加到项目插入服务请求并发送??

问题

我可以毫无问题地插入一个项目,但我不知道如何获取配料的数组列表并将其插入到配料表中。

我可以在 Azure 移动服务服务请求中将对象数组作为属性发送吗?

提前感谢您的宝贵时间。

【问题讨论】:

    标签: sql-server arrays azure middleware azure-mobile-services


    【解决方案1】:

    解决此问题的一种方法是在您的 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();
                        }
                });
            }});
        }
    

    【讨论】:

    • 哦,哇,谢谢它的工作:) 顺便说一句,我发现了一个错误。 count 变量应该初始化为 0 而不是 toppings.length
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多