简体   繁体   中英

Keen.io JS api responding {“code”: “UnknownError”} when event created OK

I'm trying to create a Keen event from parse.com cloud code (node.js). I'm using a JS module ( https://github.com/roycef/keen-parse ) which seems to be set up OK. To test things, I've set up a simple test and here is the complete main.js (credentials removed):

var express = require('express');
var app = express();

// Global app configuration section
app.use(express.bodyParser());

var Keen = require('cloud/keen.js');
var keen = Keen.configure({
    projectId: "xxxxxxxx",
    writeKey: "xxxxxxxx"
});

app.get('/kiss', function (req, res) {
    var resp = {};
    var respCode = 404;
    var testObj = {"firstname": "John", "surname": "Doe"};

    // send single event to Keen IO
    keen.addEvent("Testola", testObj, function (err, res) {
        if (err) {
            resp = err;
            respCode = 500;
        } else {
            resp = res.data;
            respCode = 200;
        }
    }).then(function () {
        // send something back to the app
        res.setHeader('Content-Type', 'application/json');
        res.send(resp, respCode);
    });
});

app.listen();

When I GET /kiss:

  • the record is stored in the relevant collection at Keen.io (yay - I can see it in the Expolorer!) however
  • the response received (err) is {"code": "UnknownError"}

So, 2 questions:

  1. why is the error response being sent when the event seems to be recorded correctly at keen.io?
  2. what can I do to get the above working?

It looks like keen-parse is using the old node-specific SDK for Keen. That SDK was deprecated quite a while ago, and I believe there have been some breaking changes in the API since then.

You probably want to use keen-js directly, instead. It's super simple to set up, and I don't think you really lose any functionality from keen-parse.

Give keen-tracking.js a try. This is a new tracking-only SDK that is a full drop-in replacement for keen-js. Here is a quick rewrite of your example code w/ the new SDK in place:

var express = require('express');
var app = express();

// Global app configuration section
app.use(express.bodyParser());

var Keen = require('keen-tracking');
var keen = new Keen({
    projectId: "xxxxxxxx",
    writeKey: "xxxxxxxx"
});

app.get('/kiss', function (req, res) {
    var resp = {};
    var respCode = 404;
    var testObj = {"firstname": "John", "surname": "Doe"};

    // send single event to Keen IO
    keen.recordEvent("Testola", testObj, function (err, res) {
      res.setHeader('Content-Type', 'application/json');
      if (err) {
        res.send(err, 500);
      }
      else {
        res.send(res, 200);
      }
    });
});

app.listen();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM