console.log(clients.typeOf)
To log the type of clients use console.log(typeof clients).
console.log(_.keys(clients))
< ['undefined']
_.key is reporting that clients has a single key, which is named "undefined". Most likely, this key-value pair is not being shown by stringify because its value is also undefined, which means it will be skipped by stringify.
To verify this, instead of
console.log('clients ' + JSON.stringify(clients))
use
console.log('clients', clients)
This will show you all the properties, including ones that stringify would skip because their value is undefined.
In your particular case, the behavior you report is best explained by a bug in buildPromiseMethod, namely that the promise it returns is being resolved to an object with a single key of "undefined` with an unserializable value. For instance, consider the following:
> clients = { undefined: undefined };
> console.log('clients ' + JSON.stringify(clients))
< clients {}
> console.log(_.keys(clients))
< ['undefined']
which is exactly what you are getting.
However, it is not obvious how buildPromiseMethod could be returning such a malformed object. The most likely culprit is zipObject. Is that really the exact source code you are running? For instance, _.zipObject([cids], data) (specifying cids as an array, when its value is undefined, while data also contains undefined values) could result in the reported behavior:
var data, data1, data2; // values are undefined
data = [data, data1, data2];
var cids = undefined;
_.zipObject([cids], data);
> { undefined: undefined }
By the way, it's likely that your use of promises is wrong, at least assuming that // fetch data is asynchronous. You probably want
static buildPromiseMethod(cids) {
if (condition) {
return fetchdata().then(data =>
_.zipObject(cids, data));
}
or something similar. This could be connected to your problem, if data1 etc. is coming back from the promise, and you are trying to access it before the promise completes in which case its value will be undefined.
_.keys(undefined).length === 0