CosmoDb Javascript:UDF中的错误getContext()

时间:2018-02-26 10:57:26

标签: javascript sql azure-cosmosdb

我有一个像这样的UDF sumItems(可能是SQL不正确)

function userDefinedFunction(purchase_order){

        var collection = getContext().getCollection();
        var response = getContext().getResponse();

        var filterQuery = 'SELECT SUM(item.unit_price * item.order_qty) FROM item IN sales.items where sales.purchase_order_number = "' + purchase_order  + '"';

        var accept = collection.queryDocuments(collection.getSelfLink(), filterQuery, {},
            function (err, responseOptions) {
                if (err) throw new Error("Error" + err.message);
            });

        return accept;

}

当我从查询中调用它时,我收到以下错误

这是查询

SELECT sales.id, udf.sumItems(sales.purchase_order_number) FROM sales

这是错误

ReferenceError:未定义'getContext'  堆栈跟踪:ReferenceError:'getContext'未在userDefinedFunction(sumItems.js:3:9)中定义

我的收藏'销售'的JSON是:

{'id' : 'o12345', 'account_number' : 'Account3',
                              'purchase_order_number' : 'PO15428132601',
                              'order_date' : '2007/07/11',
                              'due_date' : '2007/07/21',
                              'shipped_date' : '2007/07/15',
                              'subtotal' : 6107.0820,
                              'tax_amount' : 586.1203,
                              'freight' : 183.1626,
                              'discount_amt' : 1982.872,
                              'total_due' : 4893.3929,
                              'items' : [
                                  {'order_qty' : 3,
                                   'product_code' : 'A-123',      
                                   'product_name' : 'Product 1',  
                                   'currency_symbol' : '$',       
                                   'currecny_code' : 'USD',       
                                   'unit_price' : 17.1,           
                                   'line_price' : 5.7
                                  },
                                  {'order_qty' : 2,
                                   'product_code' : 'A-124',      
                                   'product_name' : 'Product 2',  
                                   'currency_symbol' : '$',       
                                   'currecny_code' : 'USD',       
                                   'unit_price' : 20,           
                                   'line_price' : 3.7
                                  }
                                  ],
                              'ttl' : 60 * 60 * 24 * 30
                              }

请帮助,thx

0 个答案:

没有答案