2016-06-23 26 views
0

我想知道如何在Azure中使用Easy API在Azure中進行查詢之後解析結果?對我來說,似乎在函數內部創建一個objet(結果)會導致無限循環,最後返回httpcode 500. 請幫忙。必須有解析結果的方法,將其存儲在一個Javascript對象中,然後將結果作爲Json返回。如何在節點js中解析azure表的結果

var azureMobileApps = require('azure-mobile-apps'); 
var queries = require('azure-mobile-apps/src/query'); 

var api = { 
     get: (req, res, next) => { 
      if(Object.keys(req.query).length === 0) { 
      res.status(400).type('text/plain').send("Error! Please add event id"); 
      return; 
      } 

     if(req.query.eventId === 'undefined' || req.query.eventId.length === 0) { 
      res.status(400).type('text/plain').send("Error! missing eventId parameter"); 
      console.log("worked!"); 
      return;  
     } 
     var query = { 
      sql: 'Select .... where [email protected]' 
      , 
      parameters: [ 
       { name: 'eventId', value: req.query.eventId } 
      ] 
     }; 

     req.azureMobile.data.execute(query) 
      .then(function (results) { 
       TODO: here! Parse results, add properties to objects and then return that instead! 
       res.status(200).type('application/json').send({sessions: results}); 
      },function(error){ 
       console.log('Found an error: ', error); 
      }); 
    } 
}; 

api.get.access = 'authenticated'; 
module.exports = api; 
+0

嗨,你現在解決了你的問題嗎?如果您有任何更新,請隨時通知我。 –

回答

2

您的代碼片段沒有任何問題。在node.js後端移動應用程序中,req.azureMobile.data.execute(query)返回查詢結果的數組列表。以下是由Azure Dev Team提供的GitHub https://github.com/Azure/azure-mobile-apps-node/blob/master/samples/custom-api-sql-stmt/api/completeall.js上的代碼示例。

Node.js中的Mobile App基於Expressjs框架。你可以簡單地使用res.json(obj)來直接返回一個json響應。

單獨爲您的問題,通常,stmt異常會引發您的問題。在查詢數據之前,請仔細檢查您的query對象是否正確。

您可以使用Visual Studio Team Services(用於Visual Studio Online)對nodejs後端移動應用程序進行故障診斷。

任何進一步concenr,請隨時讓我知道。