【发布时间】:2017-01-15 18:32:59
【问题描述】:
在这种情况下我很困惑使用书架模型向数据库插入数据:
我正在使用这些库: knex、bookshelf、express、body-parser 和 mysql 数据库
我在 db 上有一个名为 location 的表,它包含以下列:
loc_id、latitude、longitude、time
这个位置是从一个用户发送的,所以我需要用user_id将loc_id保存在另一个表中
要保存位置(单个对象)而不保存 user_id,我使用以下代码:
.post(function (req, res) {
Loc.forge({latitude: req.body.location.lat,
longitude: req.body.location.lng,
date:req.body.time,
speed:req.body.speed,
gpsAnten:req.body.gpsAnten})
.save()
.then(function (location) {
res.json({error: false, data: {id: location.get('id')}});
})
.catch(function (err) {
res.status(500).json({error: true, data: {message: err.message}});
});
});
但现在,我正在从我的设备发送如下正文请求:(它是 JSON 格式)
{
"user_id": 135,
"locations": [
{
"id": 1,
"latitude": "35.374760",
"longitude": "51.5123916",
"date": "0000-00-00 00:00:00"
},
{
"id": 2,
"latitude": "35.6247466",
"longitude": "51.51241",
"date": "0000-00-00 00:00:00"
},
{
"id": 3,
"latitude": "35.6247466",
"longitude": "51.51241",
"date": "0000-00-00 00:00:00"
},
{
"id": 4,
"latitude": "35.6247466",
"longitude": "51.51241",
"date": "0000-00-00 00:00:00"
},
{
"id": 5,
"latitude": "35.6247466",
"longitude": "51.51241",
"date": "0000-00-00 00:00:00"
},
{
"id": 6,
"latitude": "35.6247466",
"longitude": "51.51241",
"date": "0000-00-00 00:00:00"
}
]
}
如何将每个位置保存在 location 表中并获取此 ID,然后将其 loc_id 和 user_id 保存在 loc_user 表中?
【问题讨论】:
标签: mysql node.js express knex.js bookshelf.js