2017-04-21 8 views
1

私は、内部APIからget要求を受け取り、属性の割り当てを行い、JSON応答を返すNode関数を持っています。当初、私はハードコードされた定数の束を設定しましたが、これらの値をストレージテーブルに移動することにしました。そのため、関数自体の値を変更する必要はありません。将来の価格。私は、ストレージテーブルを作成するためのドキュメントに従って、それを設定し、バインドするために私の関数を設定しました。私が知る限り、正しく呼んでいますが、 "TypeError:プロパティxを読み取れません"というメッセージが表示されます。ここに私のfunction.jsは次のようになります。テーブルストレージバインディングからデータを正しく読み取る方法は?

{ 
    "bindings": [ 
    { 
     "authLevel": "function", 
     "type": "httpTrigger", 
     "direction": "in", 
     "name": "req", 
     "methods": [ 
     "GET" 
     ] 
    }, 
    { 
     "type": "http", 
     "direction": "out", 
     "name": "res" 
    }, 
    { 
     "type": "table", 
     "name": "rate", 
     "tableName": "Rates", 
     "partitionKey": "Production", 
     "rowKey": "Standard", 
     "connection": "{TABLE_CONNECTION_STRING}", 
     "direction": "in" 
    } 
    ], 
"disabled": false 
} 

そして、私のindex.jsは次のようになります。

module.exports = function (context, req) { 
    context.log('Processing mailing'); 

    if (req.query.number_of_pages && req.query.delivery_country) { 
     const first_page_special_domestic = context.bindings.rate.first_page_special_domestic; 
     const addtl_page_special_domestic = context.bindings.rate.addtl_page_special_domestic; 
     const express_domestic = context.bindings.rate.express_domestic; 
     const certified = context.bindings.rate.certified; 
     const tracking = context.bindings.rate.tracking; 
     const registered = context.bindings.rate.registered; 
     const return_envelope = context.bindings.rate.return_envelope; 
     const first_page_special_international = context.bindings.rate.first_page_special_international; 
     const addtl_page_special_international = context.bindings.rate.addtl_page_special_international; 
     const first_page_international = context.bindings.rate.first_page_international; 
     const addtl_page_international = context.bindings.rate.addtl_page_international; 
     const express_international_flat_rate = context.bindings.rate.express_international_flat_rate; 

     var number_of_pages = req.query.number_of_pages; 
     var delivery_country = req.query.delivery_country; 
     var flat_cost = 0.0; 
     var per_page_cost = 0.0; 
     var cost = 0.0; 
     var tax = 0.0; 
     var discount = 0.0; 
     var first_page_domestic = context.bindings.rate.first_page_domestic; 
     var addtl_page_domestic = context.bindings.rate.addtl_page_domestic; 

     if (req.query.rate == 1) { 
      first_page_domestic = context.bindings.rate.first_page_domestic_discount_one; 
      addtl_page_domestic = context.bindings.rate.addtl_page_domestic_discount_one; 
     } 
     else if (req.query.rate == 2) { 
      first_page_domestic = context.bindings.rate.first_page_domestic_discount_two; 
      addtl_page_domestic = context.bindings.rate.addtl_page_domestic_discount_two; 
     } 

     if (delivery_country == "US") { 
      if (req.query.special_paper) { 
       flat_cost = first_page_special_domestic; 
       per_page_cost = addtl_page_special_domestic; 
      } 
      else { 
       flat_cost = first_page_domestic; 
       per_page_cost = addtl_page_domestic; 
      } 
      if (req.query.tracking) flat_cost += tracking; 
      if (req.query.certified) flat_cost += certified; 
      if (req.query.registered) flat_cost += registered; 
      if (req.query.express) flat_cost += express_domestic; 
      if (req.query.return_envelope) flat_cost += return_envelope; 
     } 
     else { 
      if (req.query.special_paper) { 
       flat_cost = first_page_special_international; 
       per_page_cost = addtl_page_special_international; 
      } 
      else { 
       flat_cost = first_page_international; 
       per_page_cost = addtl_page_international; 
      } 
      if (req.query.express) flat_cost += express_international_flat_rate; 
      if (req.query.return_envelope) flat_cost += return_envelope; 
     } 

     if (number_of_pages > 1) { 
      cost = ((number_of_pages - 1) * per_page_cost) + flat_cost; 
     } 
     else { 
      cost = flat_cost; 
     } 

     cost = cost.toFixed(2); 

     if (req.query.state_tax == "true" && delivery_country == "US") { 
      tax = 0.095; 
     } 

     context.res = { 
      status: 200, 
      body: { "cost": cost, 
        "tax": tax 
      } 
     }; 
    } 
    else { 
     context.res = { 
      status: 421, 
      body: "Unprocessable Entity" 
     }; 
    } 
    context.done(); 
}; 

エラーメッセージが最初のconstを参照しているfirst_page_special_domestic、宣言しました。私の感覚は、私が誤って何らかの形で、function.jsで設定したことです。どんな助けもありがとう!

+0

あなたがどんなアップデートを持っていますか? –

答えて

1

基本的には、関数パラメータとしてrateを使用して値を取得できます。

以下のようにコードを変更してください:

module.exports = function (context, req, rate) { 
    context.log('Processing mailing'); 

    if (req.query.number_of_pages && req.query.delivery_country) { 
     const first_page_special_domestic = rate.first_page_special_domestic; 
     //... 
    } else { 
     //... 
    } 
    context.done(); 
}; 
関連する問題