2017-08-16 65 views
1

我需要使用http.post發送一個json格式數據到一個url。寫下面的代碼在本地測試使用http.post meteor發送json文件,然後通過http.get獲取它

var postData = { 
"channelName" : "Number Theory1", 
"startDate" : "2017-07-22T06:29:35.681Z", 
"endDate" : "2017-08-22T06:29:35.681Z" 
} 
HTTP.call('POST', 'http://localhost:3000', { 
    data: postData 
}, (error, result) => { 
if (error) { 
    console.log('we are getting this error:' + error); 
} else { 
    console.log('POstres : ' + result); 
} 
}); 
function extractProcessData(data){ 
    console.log('function called! : ' + data);  ##data here should be var processData but it prints undefined 
} 
function confirmDataReceived(data) { 
    HTTP.get('http://localhost:3000', function(err, res){ 
    // confirmation error 
    if(err){ 
    console.log('error ' + err); 
    } 
    // confirmation success and process data 
    else{ 
    console.log('data : ' + data + res) ## data here prints as undefined whereas according to me, it should contain thevar postData ## 
    extractProcessData(data) //call function to process data 
    } 
}); 
} 
var postRoutes = Picker.filter(Meteor.bindEnvironment(function(req, res) { 
if (req.method == "POST"){ 
    console.log('req : ' + req.method + " " + req.body) 
    confirmDataReceived(req.body); 
} 
return true; 
// return req.method == "POST"; 
}));;; 

它不工作。我想這個問題是與http.post數據語法。通過http.post發送數據是否正確?我希望我的http.get方法通過選擇器功能是正確的。

任何幫助,將不勝感激。

乾杯!

回答

0
var postData = { 
    "channelName" : "Number Theory1", 
    "startDate" : "2017-07-22T06:29:35.681Z", 
    "endDate" : "2017-08-22T06:29:35.681Z" 
} 
HTTP.call('POST', 'http://localhost:3000', { 
    data: postData 
}, (error, result) => { 
    if (error) { 
     console.log('we are getting this error:' + error); 
    } else { 
     console.log('POstres : ' + JSON.stringify(result)); 
    } 
}); 
+0

無法正常工作。發佈整個場景以獲得更好的理解 – user3807691

+0

確保您的依賴關係和路由正確 – 2017-08-16 12:41:54

+0

爲什麼我的代碼中存在與此相關的錯誤? afaik,我的路線和依賴關係是正確的 – user3807691

相關問題