3

I'm stumped.

This is code:

const clientInfoPromise = buildPromiseMethod
clientInfoPromise.then((clients) => {
    console.log('clients ' + JSON.stringify(clients))
    console.log(clients.typeOf)
    console.log(_.keys(clients))

This is output:

clients {}
undefined
['undefined']

I would like _.keys(clients) to return an empty array instead of an array with the string 'undefined'. _.isEmpty(clients) ? [] : _.keys(clients) doesn't work, because _.isEmpty returns false.

ClientInfoPromise is defined here:

static buildPromiseMethod(cids) {
    if (condition) {
        // fetch data
        const data = [data,data1,data2]
        return Promise.resolve(_.zipObject(cids, data));
    } else {
      const clients = _(cids)
        .indexBy()
        .mapValues(() => {
          return undefined; //contains empty data
        })
        .value();
      return Promise.resolve(clients);
    }
  }

cids can be undefined, [], or [1,2,3] (array of numbers).

2
  • Looks like you are missing the closing curly brace and parens at the end of your first code block. Commented Oct 18, 2015 at 0:51
  • hmm? _.keys(undefined).length === 0 Commented Oct 18, 2015 at 0:52

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.