0

I'm trying to combine the 2 objects and save them to the collection. But as I'm really new to Node.js/Express I think I'm missing something. All advice is welcome!

Thx in advance for your precious time.

This is what I puzzled together but doesn't work:

app.post('/:collection', function(req, res) {
    var object = req.body;
    var collection = req.params.collection;
    geocoder.geocode(req.body.address.street + req.body.address.no + req.body.address.zip + req.body.address.city, function(req, res) {
    var gpslocation = ({"loc": [res[0].longitude, res[0].latitude]});
    mergedObject = JSON.parse((JSON.stringify(object) + JSON.stringify(gpslocation)).replace(/}{/g,","))
    console.log(mergedObject);
    });
    collectionDriver.save(collection, object, function(error,docs) {
          if (error) { res.send(400, error); }
          else { res.send(201, docs);} //B
   });
});

output(console.log) but actual save is working but not with the longitude/latitude appended to it:

{ cat: 'Electrician6',
name: { first: 'John', last: 'Pipe', picture_ID: '23872' },
created_at: '2015-05-16T14:05:23.431Z',
_id: '55574ea3435c2d9c24d21572',
loc: [ 4.7261822, 50.9635574 ] }

But when I edit it to:

app.post('/:collection', function(req, res) { //A
    var object = req.body;
    var collection = req.params.collection;
    geocoder.geocode(req.body.address.street + req.body.address.no + req.body.address.zip + req.body.address.city, function(req, res) {
    var gpslocation = ({"loc": [res[0].longitude, res[0].latitude]});
    mergedObject = JSON.parse((JSON.stringify(object) + JSON.stringify(gpslocation)).replace(/}{/g,","))
    console.log(mergedObject);
    });
    collectionDriver.save(collection, mergedObject, function(error,docs) {
          if (error) { res.send(400, error); }
          else { res.send(201, docs);} //B
   });
});

output:

ReferenceError: mergedObject is not defined
    at app.put.params (/app/testapp/index.js:92:39)
    at callbacks (/app/testapp/node_modules/express/lib/router/index.js:161:37)
    at param (/app/testapp/node_modules/express/lib/router/index.js:135:11)
    at param (/app/testapp/node_modules/express/lib/router/index.js:132:11)
    at pass (/app/testapp/node_modules/express/lib/router/index.js:142:5)
    at Router._dispatch (/app/testapp/node_modules/express/lib/router/index.js:170:5)
    at Object.router (/app/testapp/node_modules/express/lib/router/index.js:33:10)
    at next (/app/testapp/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    at Object.staticMiddleware [as handle] (/app/testapp/node_modules/express/node_modules/connect/lib/middleware/static.js:55:61)
    at next (/app/testapp/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    { cat: 'Electrician6',
    name: { first: 'John', last: 'Pipe', picture_ID: '23872' },
    loc: [ 4.7261822, 50.9635574 ] }
JakeJ
  • 13
  • 1
  • 8
  • You can refer this link, and see if helps. http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically – Amit Badheka May 16 '15 at 14:05

2 Answers2

0

To merge, use the lodash merge function, it does the trick

Alexis Paques
  • 1,885
  • 15
  • 29
0

There is no reason to merge two objects. Just add the "loc" property to the original object. Also you need to call collectionDriver.save inside your callback so it happens only after geocoder.geocode is finished.

app.post('/:collection', function(req, response) {
    var object = req.body;
    var collection = req.params.collection;
    geocoder.geocode(object.address.street + object.address.no + object.address.zip + object.address.city, function(req, res) {
      object.loc = [res[0].longitude, res[0].latitude];
      console.log(object);
      collectionDriver.save(collection, object, function(error,docs) {
        if (error) { response.send(400, error); }
        else { response.send(201, docs);}
      });
    });
});
Tesseract
  • 8,049
  • 2
  • 20
  • 37
  • It works but I tried this approach earlier but the app craches with this error: /app/testapp/index.js:89 object.loc = [res[0].longitude, res[0].latitude]; ^ TypeError: Cannot read property 'longitude' of undefined at /app/testapp/index.js:89:27 at /app/testapp/node_modules/node-geocoder/lib/geocoder.js:33:17 – JakeJ May 16 '15 at 14:30
  • What error? Have you you actually tried my code? – Tesseract May 16 '15 at 14:31
  • Yes i tried it, and he saves it to the datastore a well but exits with following error: /app/testapp/index.js:89 object.loc = [res[0].longitude, res[0].latitude]; ^ TypeError: Cannot read property 'longitude' of undefined at /app/testapp/index.js:89:27 at /app/testapp/node_modules/node-geocoder/lib/geocoder.js:33:17 at Geocoder._format (/app/testapp/node_modules/node-geocoder/lib/geocoder.js:149:5) at /app/testapp/node_modules/node-geocoder/lib/geocoder.js:31:15 at /app/testapp/node_modules/node-geocoder/lib/geocoder/googlegeocoder.js:7 – JakeJ May 16 '15 at 14:32
  • Put in `console.log(res)` to see what res is. – Tesseract May 16 '15 at 14:35
  • ReferenceError: res is not defined – JakeJ May 16 '15 at 14:37
  • ReferenceError: res is not defined at app.post.object (/app/testapp/index.js:88:17) at callbacks (/app/testapp/node_modules/express/lib/router/index.js:161:37) at param (/app/testapp/node_modules/express/lib/router/index.js:135:11) at param (/app/testapp/node_modules/express/lib/router/index.js:132:11) at pass (/app/testapp/node_modules/express/lib/router/index.js:142:5) at Router._dispatch (/app/testapp/node_modules/express/lib/router/index.js:170:5) at Object.router (/app/testapp/node_modules/express/lib/router/index.js:33:10) at next ] ) – JakeJ May 16 '15 at 14:43
  • Put `console.log(res)` right above `object.loc = [res[0].longitude, res[0].latitude];` – Tesseract May 16 '15 at 14:44
  • Looks like its empty -> [] – JakeJ May 16 '15 at 14:46
  • So sorry, was something wrong with the adres. Now getting: – JakeJ May 16 '15 at 14:48
  • now getting : /app/testapp/node_modules/mongodb/lib/mongodb/connection/base.js:246 throw message; ^ TypeError: Object [object Object] has no method 'send' at /app/testapp/index.js:94:20 at /app/testapp/collectionDriver.js:67:11 at /app/testapp/node_modules/mongodb/lib/mongodb/collection/core.js:128:9 at /app/testapp/node_modules/mongodb/lib/mongodb/db.js:1195:7 at /app/testapp/node_modules/mongodb/lib/mongodb/db.js:1903:9 at Server.Base._callHandler (/app/testapp/node_modules/mongodb/lib/mongodb/connection/base.js:4 – JakeJ May 16 '15 at 14:48
  • but it gets saved to the datastore – JakeJ May 16 '15 at 14:48
  • But `response` should have a method `send`. Check again if your code is really identical to mine. Especially the use of `res` and `response`. – Tesseract May 16 '15 at 14:58
  • Thank you so much! It was another function which was giving the error. Thanks for your patience and time! – JakeJ May 17 '15 at 08:43