keen / keen-js

https://keen.io/ JavaScript SDKs. Track users and visualise the results. Demo http://keen.github.io/keen-dataviz.js/
MIT License
578 stars 143 forks source link

DELETE request returns SyntaxError: Unexpected end of JSON input #436

Closed TheCodeKing closed 6 years ago

TheCodeKing commented 7 years ago

I'm using the example in the docs to delete some events from nodejs version v7.9.0. It seems to actually succeed as the events disappear, but the delete function returns an error. I'm using keen-js version 4.3.0.

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at IncomingMessage.<anonymous> (xxx\node_modules\keen-analysis\lib\utils\http-server.js:47:27)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:188:7)
    at endReadableNT (_stream_readable.js:975:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)

Also there's an error in the documentation. You are missing the braces inside the filter array in the example code for JS. The code from your docs I'm using with braces added. Obviously I've changed the property_name and property_value in my actual code.

const client = new Keen({
  projectId: 'PROJECT_ID',
  masterKey: 'MASTER_KEY'
});

client
  .del(client.url('events', 'my-event-stream'))
  .auth(client.masterKey())
  .send({
    filters: [{
      property_name: 'user.id',
      operator: 'eq',
      property_value: 'f1243353243fdb'
    }],
    timeframe: 'this_7_days',
    timezone: 'US/Pacific'
  })
  .then(res => {
    // Handle response
  })
  .catch(err => {
    // Handle error
    // SyntaxError: Unexpected end of JSON input
  });
TheCodeKing commented 7 years ago

Actually, I just realised the observed behaviour is that all items are deleted and the filter is ignored.

dustinlarimer commented 7 years ago

@TheCodeKing - thanks for reporting! We'll get that syntax error fixed up. I think I've found the issue with the DELETE request as well, and will open a separate issue.

dustinlarimer commented 7 years ago

@TheCodeKing this example should work properly.. apologies for the error!

import Keen from 'keen-js';

const client = new Keen({
  projectId: 'PROJECT_ID',
  masterKey: 'MASTER_KEY'
});

/*
  Filters and timeframe must be passed as encoded query string parameters. 
  This example constructs a complete URL to ensure the request is executed properly.
*/
const url = client.url('events', 'my-event-stream', {
  api_key: client.masterKey(),
  filters: encodeURIComponent(JSON.stringify([
    {
      property_name: 'user.id',
      operator: 'eq',
      property_value: 'f1243353243fdb'
    }
  ])),
  timeframe: encodeURIComponent(JSON.stringify({
    start: '2015-05-15T19:00:00.000Z',
    end: '2015-06-07T19:00:00.000Z'
  })),
  timezone: 'US/Pacific'
});

client
  .del(url)
  .send()
  .then(res => {
    // Handle response
  })
  .catch(err => {
    // Handle error
  });
adamkasprowicz commented 6 years ago

fixed in keen-analysis@1.3.2, keen-js@4.3.1