Node.js 如何更新不可用的JSON字段';t在Sails.js中创建
我有一个模型:Place,当我在Mongodb数据库中创建记录时,我设置了一些字段,例如: place_名称、描述、容量和JSON place_地址中的所有属性,而不是纬度和经度 在PlaceController.js中,我有一个更新方法,我想在JSON place\u地址中更新未设置的纬度和经度字段 唯一的方法是创建一个新的JSON place_地址,在这里我获取所有以前的参数并设置纬度和经度 有没有更简单的方法可以做到这一点,比如:(但是下面的代码不起作用) 在PlaceController.js中Node.js 如何更新不可用的JSON字段';t在Sails.js中创建,node.js,mongodb,express,sails.js,Node.js,Mongodb,Express,Sails.js,我有一个模型:Place,当我在Mongodb数据库中创建记录时,我设置了一些字段,例如: place_名称、描述、容量和JSON place_地址中的所有属性,而不是纬度和经度 在PlaceController.js中,我有一个更新方法,我想在JSON place\u地址中更新未设置的纬度和经度字段 唯一的方法是创建一个新的JSON place_地址,在这里我获取所有以前的参数并设置纬度和经度 有没有更简单的方法可以做到这一点,比如:(但是下面的代码不起作用) 在PlaceController
Place.update(id, {
place_address.latitude : req.param('latitude'),
place_address.longitude : req.param('longitude')
})
为了更好地理解,这里是模型Place.js:
module.exports = {
attributes: {
place_name : {
type : 'string',
required : true,
},
description : {
type : 'string',
required : true,
},
capacity : {
type : 'string',
required : true,
},
place_address : {
type : 'json',
required : true,
},
office_hours: {
type : 'array'
},
conditions: {
type : 'json'
},
functions: {
type : 'array'
},
owners: {
type : 'array'
}
}
};
update: function (req,res) {
var id = req.params.id;
var placeJSON = {street_number: req.param('street_number'), street_name: req.param('street_name'), additional_info: req.param('additional_info'), postcode: req.param('postcode'), town: req.param('town'), country: req.param('country')};
if (!id) {
console.log("No id specified.");
return res.send("No id specified.",500);
}
Place.update(id, {
place_name : req.param('place_name'),
description : req.param('description'),
capacity : req.param('capacity'),
place_address : placeJSON
}).exec(function (err, updatedPlace) {
if (err) {
res.redirect('/place/edit');
}
if(!updatedPlace) {
res.redirect('/place/edit');
}
//res.redirect('/place');
res.redirect('/place/show/'+id);
});
}
这里是ControllerPlaceController.js:
module.exports = {
attributes: {
place_name : {
type : 'string',
required : true,
},
description : {
type : 'string',
required : true,
},
capacity : {
type : 'string',
required : true,
},
place_address : {
type : 'json',
required : true,
},
office_hours: {
type : 'array'
},
conditions: {
type : 'json'
},
functions: {
type : 'array'
},
owners: {
type : 'array'
}
}
};
update: function (req,res) {
var id = req.params.id;
var placeJSON = {street_number: req.param('street_number'), street_name: req.param('street_name'), additional_info: req.param('additional_info'), postcode: req.param('postcode'), town: req.param('town'), country: req.param('country')};
if (!id) {
console.log("No id specified.");
return res.send("No id specified.",500);
}
Place.update(id, {
place_name : req.param('place_name'),
description : req.param('description'),
capacity : req.param('capacity'),
place_address : placeJSON
}).exec(function (err, updatedPlace) {
if (err) {
res.redirect('/place/edit');
}
if(!updatedPlace) {
res.redirect('/place/edit');
}
//res.redirect('/place');
res.redirect('/place/show/'+id);
});
}
populate
sails功能可帮助您表达集合之间的关联,在您的情况下,位置和坐标可以如下所示:
// create new place and coordinates
Place.create(placeParams).then((place) => {
Coordinate.create(coordianteParams).then((coordinate) => {
Place.findOne(lesson.id).populate('coordinates').then((place) => {
res.json(place);
})
})
// get place and coordinates
Place.find({}}.popupale('coordinates').then((places) => {
res.json(places)
});
这种方法可以单独更新坐标并将其重新附加到父对象上(placecollection,在这种情况下)建议对内部json对象使用@AkramSaouri已经编写的
populate
函数-创建其他模型并使用Association,然后填充存储库!我希望有一种更简单的方法,比如在MongoDB驱动程序中使用“$push”将某些内容推送到JSON数组中。但你的方法更像是“航行方式”,所以谢谢你;)很高兴我能帮助你,哥们,祝你好运