2015-03-03 69 views
0

我新的網絡掛接,並試圖找出如何接收一個JSON對象並將其保存到Parse.com發佈JSON的網絡掛接上Parse.com

這裏就是我的Express.js網絡掛接的樣子:

// These two lines are required to initialize Express in Cloud Code. 
var express = require('express'); 
var app = express(); 

// Global app configuration section 
app.set('views', 'cloud/views'); // Specify the folder to find templates 
app.set('view engine', 'ejs'); // Set the template engine 
app.use(express.bodyParser()); // Middleware for reading request body 

app.post('/notify_message', 
     // express.basicAuth('', ''), 
     function(data) { 
    // Use Parse JavaScript SDK to create a new message and save it. 
    var Kenpom = Parse.Object.extend("Kenpom"); 
    var kenpom = new Kenpom(); 
    kenpom.save({ 
    conference : data.results, 
    }).then(function(kenpom) { 
    res.send('Success'); 
    }, function(error) { 
    res.status(500); 
    res.send('Error'); 
    }); 
}); 

// Attach the Express app to Cloud Code. 
app.listen(); 

這是越來越發送到網絡掛接JSON對象:

{ 
    "name": "REST", 
    "count": 351, 
    "frequency": "Every 15 mins", 
    "version": 198, 
    "newdata": false, 
    "lastrunstatus": "success", 
    "lastsuccess": "Tue Mar 03 2015 02:41:02 GMT+0000 (UTC)", 
    "thisversionstatus": "success", 
    "nextrun": "Tue Mar 03 2015 03:29:21 GMT+0000 (UTC)", 
    "thisversionrun": "Tue Mar 03 2015 02:41:02 GMT+0000 (UTC)", 
    "results": { 
    "collection1": [ 
     { 
     "rank": "1", 
     "team": { 
      "href": "http://kenpom.com/team.php?team=Kentucky", 
      "text": "Kentucky" 
     }, 
     "conference": { 
      "href": "http://kenpom.com/conf.php?c=SEC", 
      "text": "SEC" 
     }, 
     "currentrecord": "29-0", 
     "pyth": ".9790", 
     "offensiveefficiency": "118.7", 
     "defensiveefficiency": "84.9", 
     "tempo": "63.6" 
     }, 
     { 
     "rank": "2", 
     "team": { 
      "href": "http://kenpom.com/team.php?team=Arizona", 
      "text": "Arizona" 
     }, 
     "conference": { 
      "href": "http://kenpom.com/conf.php?c=P12", 
      "text": "P12" 
     }, 
     "currentrecord": "26-3", 
     "pyth": ".9649", 
     "offensiveefficiency": "115.8", 
     "defensiveefficiency": "86.8", 
     "tempo": "66.6" 
     }, .... 

Parse.com正在接收所有列,但我看到在解析對象就是:{}

+0

這個數據來自KimonoLabs.com嗎?我試圖使用Parse.com來webhook來自KimonoLabs的數據。我不知道如何提供從Parse到KimonoLabs的webhook網址。 – 2015-10-25 03:58:23

回答

0

您必須缺少代碼中的主體解析器設置。這裏是一個示例代碼,在我的情況下完美工作。看線3至5

var Express = require('express'); 
var app = Express(); 
var parseExpressRawBody = require('parse-express-raw-body'); 
app.use(Express.bodyParser()); // Populate req.body 
app.use(parseExpressRawBody()); //For parsing the body in JSON format 

app.post('/createMessage', function(req,res){ 
    var data = req.body; 
    var Message = Parse.Object.extend("Message"); 
    var message = new Message(); 
    message.save({ 
     type : data.type, 
     text : data.text 
    }).then(function(message) { 
     res.send("Success"); 
    }, function(error) { 
     res.status(500); 
     res.send('Error'); 
    }); 
}); 

app.listen(); 

注:您應該包含頭「內容類型:應用/ JSON」在來自客戶機的HTTP POST請求。

希望這可以解決您的問題。

+0

這不起作用。現在我在Parse的表格中沒有獲得任何回報。 – beaconhill 2015-03-04 02:25:08