kuzzleio / sdk-javascript

Kuzzle Javascript SDK. High level library including multi-protocol support, offline resiliency, realtime data and more
http://docs.kuzzle.io/sdk/js/7
Apache License 2.0
39 stars 15 forks source link

[6.0.0-beta2] HTTP NetworkHandler does not work as expected #324

Closed stafyniaksacha closed 5 years ago

stafyniaksacha commented 5 years ago

I've tried to switch network handler in a working application using websocket network handler, and every thing hangs up

Expected Behavior

The HTTP network handler should work like websoket one (without realtime)

Current Behavior

At request creation time, queryArgs are populated with SDK default options which contains undefined or null values that are included in queryArgs as is. Kuzzle trait them as string.

Steps to Reproduce

When we create a document like something:

await kuzzle.document.create(
  "index",
  "colection",
  null,
  awesomeDocument,
  {
    refresh: "wait_for"
  }
);

The document ID became "null" The scroll parameter became "undefined" etc ...

Possible Solution

In send method on networkWrapper/protocols/http.js, but need to be tested

for (const key of Object.keys(data)) {
  const value = data[key];

  if (key === 'body') {
    payload.body = JSON.stringify(value);
  } else if (key === 'jwt') {
    payload.headers.authorization = 'Bearer ' + value;
  } else if (key === 'volatile') {
    payload.headers['x-kuzzle-volatile'] = JSON.stringify(value);
  } else if (payload.hasOwnProperty(key)) {
    payload[key] = value;
-  } else {
-    queryArgs[key] = value;
-  }
+  } else if (value !== undefined && value !== null) {
+    queryArgs[key] = value;
+  }
}
Aschen commented 5 years ago

Hello @stafyniaksacha, thanks for the feedback!

I didn't manage to reproduce your error, here you can see my code: https://gist.github.com/Aschen/6645445ab6d5608611f85e004fb4f1d9 I run this snippet in the sdk-javascript folder locally

stafyniaksacha commented 5 years ago

I run your code and I still reproduce the issue:

{ _index: 'index',
  _type: 'collection',
  _id: 'null',
  _version: 1,
  found: true,
  _source: 
   { hello: 'world',
     _kuzzle_info: 
      { author: '-1',
        createdAt: 1540739055056,
        updatedAt: null,
        updater: null,
        active: true,
        deletedAt: null } },
  _meta: 
   { author: '-1',
     createdAt: 1540739055056,
     updatedAt: null,
     updater: null,
     active: true,
     deletedAt: null } }

the problem is there: _id: 'null' got the same issue when trying to play with from/size options on document.search

View kuzzle.server.info() output

_Got same issue with redis 3.2_ ``` { serverInfo: { kuzzle: { version: '1.5.2', api: { routes: { auth: [Object], bulk: [Object], collection: [Object], document: [Object], index: [Object], ms: [Object], memoryStorage: [Object], realtime: [Object], security: [Object], server: [Object], admin: [Object] } }, nodeVersion: 'v8.11.3', memoryUsed: 67186688, uptime: '5483.208s', plugins: { 'kuzzle-plugin-auth-passport-local': { manifest: [Object], hooks: [], pipes: [], controllers: [], routes: [], strategies: [Array] }, 'kuzzle-plugin-logger': { manifest: [Object], hooks: [Array], pipes: [], controllers: [], routes: [], strategies: [] } }, system: { memory: { total: 16831307776, free: 11477155840 }, cpus: [ [Object], [Object], [Object], [Object] ] } }, services: { internalCache: { type: 'redis', version: '5.0.0', mode: 'standalone', memoryUsed: '967.97K', memoryPeak: '967.97K' }, storageEngine: { type: 'elasticsearch', version: '5.6.12', lucene: '6.6.1', status: 'yellow', nodes: { count: [Object], versions: [Array], os: [Object], process: [Object], jvm: [Object], fs: [Object], plugins: [], network_types: [Object] }, spaceUsed: '8.4mb' }, memoryStorage: { type: 'redis', version: '5.0.0', mode: 'standalone', memoryUsed: '967.97K', memoryPeak: '967.97K' } } } } ```