Node.js /表示解析数据并仍然返回[object Object]作为响应并且缺少一些数据

时间:2017-09-21 17:24:32

标签: javascript node.js parsing express

如上所述,我正在使用NODE / Express后端,并在将其发送到前端之前尝试解析一些数据。

我有一个对象(项目)数组,并希望解析每个项目中的某个字段,尤其是descriptiongeolocation。它似乎工作得很好,但我遇到了一些问题。

首先,我将向您展示其中一项显示之前的内容:

{
    "_id": {
        "$oid": "59925302e12872a81b28099e"
    },
    "offer": "19f5d9ef37b30311",
    "txid": "389eb024f8869d787954c74d1653b8358e581cb4c81358361ffac662875bceec",
    "expires_in": 13770133,
    "expires_on": 1516531809,
    "expired": false,
    "height": "46411",
    "category": "for sale",
    "title": "Jack Garratt Signed Deluxe CD",
    "quantity": "1",
    "currency": "USD",
    "sysprice": 415240000000,
    "price": "35.00",
    "ismine": false,
    "commission": "0",
    "offerlink": "false",
    "private": "No",
    "paymentoptions": 2,
    "paymentoptions_display": "BTC",
    "alias_peg": "sysrates.peg",
    "description": "{\"description\":\"Signed, near mint double CD Only played a very few times\",\"media\":{\"defaultIdx\": 0,\"mediaVault\": [{\"mediaType\":\"img\",\"mediaURL\":\"http://i.imgur.com/bB7QjDR.jpg\"}]}}",
    "alias": "Signed, near mint double CD Only played a very few times http://i.imgur.com/bB7QjDR.jpg",
    "address": "1GR389Tki2LS3jScFUhrVxVnXdPdED5839",
    "alias_rating_display": "0.0/5 (0 Votes)",
    "offers_sold": 0,
    "geolocation": "{\"coords\":{\"lat\":36.8518706,\"lng\":-123.5029326}}",
    "alias_rating_count": 0,
    "alias_rating": 0,
    "safetylevel": 0,
    "safesearch": "Yes",
    "offerlink_seller": "",
    "offerlink_guid": "",
    "time": {
        "$date": "1970-01-18T04:29:58.326Z"
    },
    "cert": "",
    "__v": 0
}

接下来,我将向您展示如何解析我想要的数据。我特别想要一个description geolocation和一个新字段media

  let newResults = [];
  let updatedItem = {};
  results.map((item, i) => {
    const newDescription = JSON.parse(item.description);
    const newGeolocation = JSON.parse(item.geolocation);
    console.log(newGeolocation)
    updatedItem = item;
    updatedItem.geolocation = newGeolocation;
    updatedItem.media = newDescription.media;
    updatedItem.description = newDescription.description;

    newResults.push(updatedItem);

    return newResults;
  });

  console.log(newResults[24]);

以下是console.log(newResults[24])的结果:

{ _id: 59925302e12872a81b2809a3,
  offer: '17fff08820c6da06',
  txid: 'f27ec82c4cd694ecfdf061ebff7709a6154e39767595f7da08e4b2a40503c816',
  expires_in: 26828208,
  expires_on: 1529589884,
  expired: false,
  height: '276435',
  category: 'for sale',
  title: 'Marijuana Flavoured Vape E-Liquid 10ml 6mg',
  quantity: '19',
  currency: 'GBP',
  sysprice: 1912000000,
  price: '2.00',
  ismine: false,
  commission: '0',
  offerlink: 'false',
  private: 'No',
  paymentoptions: 1,
  paymentoptions_display: 'SYS',
  alias_peg: 'sysrates.peg',
  description: 'Marijuana Flavoured E-Liquid 10ml 6mg',
  alias: 'Marijuana Flavoured E-Liquid 10ml 6mg',
  address: '1DNeg3CrRFx6PuLcCry26p9y2XiTzTTFqw',
  alias_rating_display: '0.0/5 (0 Votes)',
  __v: 0,
  offers_sold: 0,
  geolocation: '[object Object]',
  alias_rating_count: 0,
  alias_rating: 0,
  safetylevel: 0,
  safesearch: 'Yes',
  offerlink_seller: '',
  offerlink_guid: '',
  time: Sun Jan 18 1970 02:32:37 GMT-0600 (Central Standard Time),
  cert: '' }

正如你所看到的那样,数据似乎已经被解析了,但是地理定位给了我一个奇怪的[object Object],即使我在控制台中将它记录在地图中它给了我每个解析的数据和他们看起来很好。

我想在此注意的其他内容是媒体领域甚至不存在。但是,如果我console.log(newResults[24].media),它会向我显示已解析的数据。但是,如果我尝试在前端访问它,如item.media,我会得到未定义的,因为它没有出现。

2 个答案:

答案 0 :(得分:1)

您滥用map()map()将返回一个对象。尝试使用这样的东西:

let newResults = results.map((item, i) => {
    const {
        media,
        description
    } = JSON.parse(item.description);

    const geolocation = JSON.parse(item.geolocation);

    return {
        geolocation,
        media,
        description
    };
});

console.log(newResults[24]);

如果要将这些属性添加到项目上(不改变原始项目),可以使用Object.assign()将第二个参数中的对象的所有属性转储到第一个参数优先级的任何属性中给后来的论点。

换句话说,我们将返回调用返回到Object.assign({}, item, {...new props...})的值,并且该调用将创建一个新的对象{},然后从item转储所有属性进入那个新的Object然后它将从{...new props...}转发{}第一个let newResults = results.map((item, i) => { const { media, description } = JSON.parse(item.description); const geolocation = JSON.parse(item.geolocation); return Object.assign({}, item, { geolocation, media, description }); }); 参数,并覆盖属性(如果它们已经存在)。

console.log()

此外,在带有let tmp = [ { foo: "Bar", fizz: [ { buzz: "hello" } ] } ]; console.log(tmp); 的node.js中打印嵌套对象时,节点将截断输出而不是打印整个arrary。请考虑以下代码:

[ { foo: 'Bar', fizz: [ [Object] ] } ]

将打印:

console.log(tmp[0].fizz[0].buzz);

该对象仍然正常,如果您尝试使用hello取消引用嵌套对象本身,您将获得输出:*args

答案 1 :(得分:0)

尝试

results.map((item, i) => {
  const newDescription = JSON.parse(item.description);
  const newGeolocation = JSON.parse(item.geolocation);
  console.log(newDescription.media)
  updatedItem = item;
  updatedItem.geolocation = item.geolocation; //just assign geolocation
  updatedItem.media = JSON.stringify(newDescription.media); //stringify the media object
  updatedItem.description = newDescription.description;

  newResults.push(updatedItem);

  return newResults;
});