2015-08-26 95 views
0

閱讀res.json我有一個簡單的NodeJS獲得運行這樣的:在應用程序的NodeJS

app.get('/customer/:ent_cust_id', function (req, res, next) { 
     var query = 'Select * from entcustinfo where ent_cust_id = ' + req.params.ent_cust_id; 
     console.log('Select * from entcustinfo where ent_cust_id =' + req.params.ent_cust_id); 
     client.execute(query, function (err, result) { 
     if (err) return next (err); 
     var row = result.rows[0]; 
      if (result.rows.length==0) { 
       res.send('ent_cust_id: ' + req.params.ent_cust_id + ' not found. Not all data is loaded.'); 
     } else { 
      var row = result.rows[0]; 
       //Response 
       res.json({ent_cust_id: req.params.ent_cust_id, accts: row.get('accts'), offers: row.get('offers')}); 
     }; 
     }); 
}); 

並將結果返回到一個這樣的數組:

{"ent_cust_id":"1013686356", 
     "accts":{ 
      "3017":{"popn_typ_cd":"CNSMR_DIRCT_CHKG","prod_type_cd":"DDA","ent_prod_cd":"CNSDD","curr_amt":"14.67","bal_90_days":null,"region":"NULL","trst_acct_ind":0,"legal_ind":0,"emp_acct_ind":1,"mnor_ind":0}, 
      "2752":{"popn_typ_cd":"CNSMR_LENDG","prod_type_cd":"MLA","ent_prod_cd":"CNSML","curr_amt":null,"bal_90_days":null,"region":"NULL","trst_acct_ind":0,"legal_ind":0,"emp_acct_ind":1,"mnor_ind":1}, 
      "888":{"popn_typ_cd":"CNSMR_DIRCT_MORT","prod_type_cd":"MLA","ent_prod_cd":"CNSML","curr_amt":null,"bal_90_days":null,"region":"NULL","trst_acct_ind":0,"legal_ind":0,"emp_acct_ind":1,"mnor_ind":0} 
      }, 
     "offers":{ 
      "5998":{"contacted":"Y","hit_home_date":"2015-06-03T04:00:00.000Z","creative":"Statement Message","channel":"LIST","campaign":"STMT_MESSAGE","campaign_tp":"STATEMENT"}, 
      "6998":{"contacted":"Y","hit_home_date":"2015-07-03T04:00:00.000Z","creative":"Statement Message","channel":"LIST","campaign":"STMT_MESSAGE","campaign_tp":"STATEMENT"}, 
      "7998":{"contacted":"Y","hit_home_date":"2015-08-03T04:00:00.000Z","creative":"Statement Message","channel":"LIST","campaign":"STMT_MESSAGE","campaign_tp":"STATEMENT"}, 
      "506A":{"contacted":"Y","hit_home_date":"2015-06-18T04:00:00.000Z","creative":"Availability CIT","channel":"EM","campaign":"FUNDS_AVAILABILITY_CIT","campaign_tp":"NOTIFICATION"}, 
      "06_A":{"contacted":"Y","hit_home_date":"2015-06-29T04:00:00.000Z","creative":"Mob5X","channel":"EM","campaign":"EMAIL","campaign_tp":"UPSELL"}, 
      "5009":{"contacted":"Y","hit_home_date":"2015-06-05T04:00:00.000Z","creative":"e Reminder","channel":"EM","campaign":"STMT_REMINDER","campaign_tp":"STATEMENT"}, 
      "6009":{"contacted":"Y","hit_home_date":"2015-07-09T04:00:00.000Z","creative":"e Reminder","channel":"EM","campaign":"STMT_REMINDER","campaign_tp":"STATEMENT"}, 
      "7996":{"contacted":"Y","hit_home_date":"2015-08-10T04:00:00.000Z","creative":"e Reminder","channel":"EM","campaign":"STMT_REMINDER","campaign_tp":"STATEMENT"}, 
      "R_33S":{"contacted":"Y","hit_home_date":"2015-08-10T04:00:00.000Z","creative":"1Offer","channel":"DM","campaign":"TIERED CASH AUG SEP","campaign_tp":"SELL"}, 
      "R_34S":{"contacted":"Y","hit_home_date":"2025-12-31T05:00:00.000Z","creative":"1Offer","channel":"DM","campaign":"TIERED CASH AUG SEP","campaign_tp":"SELL"}, 
      "R_22":{"contacted":"Y","hit_home_date":"2015-06-22T04:00:00.000Z","creative":"1Offer","channel":"EM","campaign":"TIERED CASH","campaign_tp":"SELL"} 
      } 
    } 

我怎麼看我可以獲得數組的第一級,但是如果我想獲得數組的其他部分呢。

如果我還想檢查offers中的contacted字段是否標記爲Y,那該怎麼辦?

我該怎麼做?

編輯:

好像這只是一個字符串,而不是一個數組。所以也許問題是,我該如何將其轉換爲數組和/或json對象?

+1

要清楚,輸出中沒有數組,只是一個對象。獲取第二個和第三個級別與獲取第一個數據沒有什麼不同。'data.offers ['5998']。contacts'獲取首先, –

+0

你的查詢似乎也容易受到sql注入的影響,如果參數等於'1;從entcustinfo中刪除,你可能會遇到問題。 –

回答

1

首先,我沒有看到任何數組,所以我對第一個問題有點困惑?

但在回答你的第二個問題,如果你的數據已經被評估成稱爲「數據」的對象,這是上面的JSON的數組,你可以這樣做:

var custId = "1013686356"; 
var determineIfContacted = function(custId, data) { 
    var customers = data.filter(function(item) { return item.ent_cust_id == custId; }) 
    var customer = customers[0]; // you should validate that you have a matching customer here 
    var hasBeenContacted = false; 
    foreach (var offerId in customer.offers) { 
     if (customer.offers[offerId].contacted === "Y") { 
      hasBeenContacted = true; 
      break; 
     } 
    } 
    return hasBeenContacted; 
} 

希望這有助於( :