我正在尝试将两个对象组合在一起并将它们保存到集合中。但由于我是Node.js / Express的新手,我觉得我错过了一些东西。欢迎提出所有建议!
提前获得宝贵的时间。
这是我一直困惑但 不 的工作:
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
});
});
输出(console.log)但是实际保存工作但没有附加经度/纬度:
{ 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 ] }
但是当我编辑它时:
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
});
});
输出:
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 ] }
答案 0 :(得分:0)
要合并,请使用lodash merge function,它可以解决问题
答案 1 :(得分:0)
没有理由合并两个对象。只需添加" loc"属性到原始对象。你还需要在回调中调用collectionDriver.save,这样只有在geocoder.geocode完成后才会发生。
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);}
});
});
});