Saturday, 8 April 2017

I am having trouble with trying to return nested objects. Can somebody figure out the mistake?

I have this "messages" object that contains information about messages, like from, to etc. What I am trying here is to return an object with the directory and the selected message.Following is the code:

module.exports.findInbox = function (db, encodedName) {
  var messages = db.messages

  return {
    dir: path.dirname(db.file),
    messages: Object.keys(messages).reduce(function (acc, key) {
      if (messages[key].to === encodedName) {
        return acc.concat({
          hash: key,
          lastHash: messages[key].last,
          from: messages[key].from
        })
      } else { return acc }
    }, [])
  }
}

But somehow I get an empty "messages" array. Can somebody please correct the mistake I am making. The json that actually gets stored in the variable messages is the following:

{
  "60b725f10c9c85c70d97880dfe8191b3": {
    "to": "QGJpZ2JpcmQ4OA==",
    "from": "QHRoZVJlYWxFbG1v",
    "last": null
  },
  "3b5d5c3712955042212316173ccf37be": {
    "to": "QHRoZVJlYWxFbG1v",
    "from": "QGFsaWNl",
    "last": null
  },
  "2cd6ee2c70b0bde53fbe6cac3c8b8bb1": {
    "to": "QGJpZ2JpcmQ4OA==",
    "from": "QHRoZVJlYWxFbG1v",
    "last": "60b725f10c9c85c70d97880dfe8191b3"
  },
  "e29311f6f1bf1af907f9ef9f44b8328b": {
    "to": "QGFsaWNl",
    "from": "QHRoZVJlYWxFbG1v",
    "last": "3b5d5c3712955042212316173ccf37be"
  }
}

Any help would be appreciated.



via Mean Stack

No comments:

Post a Comment