2016-05-13 42 views
0

局部變量有一段代碼,需要做到以下幾點:我如何訪問外

  1. 對於陣列中的每個傳感器的對象,首先獲取模板ID。
  2. 搜索模板模式數據庫,獲取相應模板ID的郵政編碼。
  3. 從郵政編碼,生成的URI
  4. 充分利用requestAPI呼叫
  5. 獲取API的輸出,並將其存儲在數據庫中該傳感器對象。

我有問題,第5步,將結果存儲在每個SenObject的DB中。我猜這個問題是因爲newSensorObjectRes是在test()本地定義的,所以不能在外面使用。我還可以如何存儲每個對象的結果?

var arr = []; 
function SenObj (id) 
{ 
    this.Objnum = 10; 
    this.Template = id; 
    this.Type = "AirFlow"; 
    this.UserID = "Jessi"; 
} 
// To store the results from the AIR API 
var sensorResults = new Schema({ 
    //reqId: {type: Number, required: true, unique: true}, 
    //sensorId: {type: Number}, 
    SenObj: {type: Number}, 
    status: {type: String, default: 'Undefined'} 
}) 
var SensorObjectRes = connUserSensors.model('SensorObjectRes', sensorResults) 

//API:To create Sensor Objects when user requests for template 
// When user makes a request to create a new Template,we get the tempate ID he has requested. 
// We use the ID to create a Sensor Object.We might need the request id too here ?? 
// The array arr[] holds all the sensor obects.. 

app.get('/ProvisionTemplate/:id', function (req, res) { 
    var id = req.params.id; 
    console.log("Server recieved a GET /ProvisionTemplate/" + id + " request"); 
    Template.findOne({templateId: parseInt(id)},function (err, data) { 
    if (err) return console.error(err); 
    console.log(data); 
    res.json(data); 
    // Create an Object here 
    var user1 = new SenObj(id); 
    console.log(user1.UserID); 
    arr.push(user1); 
    }); 
}); 
console.log("The array objects are :"); 
for (i = 0; i < arr.length; i++) 
{ 
    console.log(arr[i]); 
} 
var output = "" 
function test() 
{ 
    var zip = ""; 
    console.log("Interval reached"); 
    for (i = 0; i < arr.length; i++) 
    { 
    // For each Sensor objects in the array, get the Template ID first. 
    // Search the Template Schema DB, get the zipcode of the corresponsing Template ID got. 
    // From the zipcode, generate the URI 
    // Make the requestAPI call 
    // Get the output of the API and store it in the DB for that sensor object. 
    console.log(arr[i]); 
    console.log(arr[i].Template); 
    var tem = arr[i].Template; 
    Template.findOne({templateId: tem},function (err, data) 
    { 
     if (err) return console.error(err); 
     console.log(data.zipcode); 
     zip = data.zipcode; 
     var uri_1 = "http://www.airnowapi.org/aq/observation/zipCode/current/?format=application/json&zipCode="; 
     var uri_2 = "&distance=25&API_KEY=1035C2AC-CDB8-4540-97E4-0E8D82BA335A"; 
     var url = uri_1 + zip + uri_2; 
     console.log(url); 
     requestApi(url, function (error, response, body) 
     { 
     if (!error && response.statusCode == 200) 
     { 
      console.log(body); 
      //console.log(arr[i]) 
      var newSensorObjectRes = new SensorObjectRes({"SenObj": 1 ,"status": body}); 
      newSensorObjectRes.save(function (err, data) 
      { 
      if (err) return console.log("error updating"); 
       console.log(data); 

      }) 
     } 
     }) 

    }); 
    } 
} 
var interval = setInterval(test, 10000); 


newSensorObjectRes.find(function (err, data) 
{ 
    if (err) 
    { 
    console.log("Could not find EC2Server db"); 
    return; 
    } 
    console.log(data) 
}) 
+2

你使用的很多API都是異步的,但你似乎期望它們是同步的。我推薦閱讀[爲什麼我的變量在函數內部修改後沒有改變? - 異步代碼參考](http://stackoverflow.com/q/23667086/218196),我認爲這是你的問題。但是你也是對的,'newSensorObjectRes'不能在'test'之外使用。問題是,你爲什麼想要? –

+0

想法是將結果存儲在數據庫(newSensorObjectRes)中,稍後將在程序中調用該數據庫以查看收集的數據。否則,我該怎麼做?請幫助 –

回答

0

我相信問題是在你的測試函數中有for循環。在獲得回覆之前,您可能正在製作所有的模板請求。嘗試使用遞歸來代替。這將確保您的請求有序進行。像這樣的東西......`

function test(i) 
{ 
    if(i == arr.length){ 
    console.log("done"); 
    return; 
    } 
    var zip = ""; 
    console.log("Interval reached"); 
    console.log(arr[i]); 
    console.log(arr[i].Template); 
    var tem = arr[i].Template; 
    Template.findOne({templateId: tem},function (err, data) 
    { 
    if (err) return console.error(err); 
    console.log(data.zipcode); 
    zip = data.zipcode; 
    var uri_1 = "http://www.airnowapi.org/aq/observation/zipCode/current/?format=application/json&zipCode="; 
    var uri_2 = "&distance=25&API_KEY=1035C2AC-CDB8-4540-97E4-0E8D82BA335A"; 
    var url = uri_1 + zip + uri_2; 
    console.log(url); 
    requestApi(url, function (error, response, body) 
    { 
     if (!error && response.statusCode == 200) 
     { 
     console.log(body); 
     var newSensorObjectRes = new SensorObjectRes({"SenObj": 1 ,"status": body}); 
     newSensorObjectRes.save(function (err, data) 
     { 
      if (err) return console.log("error updating"); 
      console.log(data); 
      test(i+1); 
     }) 
     } 
    }) 
    }); 
} 
test(0); 
+0

感謝您的解決方案 –