将JSON发布到Parse.com上的webhook

时间:2015-03-03 03:54:05

标签: javascript json express parse-platform

我是webhooks的新手并试图找出如何接收JSON对象并将其保存到Parse.com

这是我的Express.js webhook的样子:

// 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();

这是发送到webhook的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正在接收所有列,但我在Parse中看到的对象只是:{}

1 个答案:

答案 0 :(得分: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();

注意:您应该包含标题' Content-Type:application / json'在来自客户端的HTTP POST请求中。

希望这可以解决您的问题。