Node.js 如何在node express mongo中定位数据
我使用以下模式来存储坐标:Node.js 如何在node express mongo中定位数据,node.js,express,Node.js,Express,我使用以下模式来存储坐标: const mongoose = require('mongoose'); const Schema = mongoose.Schema; // Create Schema const LocationSchema = new Schema({ // Matches loc: { type: { type: String }, coordinates: [Num
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema
const LocationSchema = new Schema({
// Matches
loc: {
type:
{
type: String
},
coordinates: [Number]
},
member_id: {
type: String
}
});
LocationSchema.index({ loc: '2dsphere' });
mongoose.model('locations', LocationSchema);
我还创建了一个get路径,以到达要显示存储坐标数据的页面。当我点击路由时,我在console.log中得到以下内容,我正在使用它进行调试:
{ loc: { coordinates: [ 74.360106, 31.497754 ], type: 'Point' },
_id: 5b5abd4bdcbd0f4e5c70b799,
member_id: 'HyQUa4_N7',
__v: 0 }
这对我来说很好,因为我正在获取坐标和成员ID。以下是我获取路线的代码,我使用该代码将数据传递到我的express Handlebar视图:
// Manage Location
router.get('/userlocation/:member_id', ensureAuthenticated, (req, res) => {
Location.findOne({ member_id: req.params.member_id })
.then(locations => {
console.log(locations);
res.render('users/userlocation', { locations: locations });
})
.catch(error => {
console.log('could not find user location');
});
});
我对如何在HTML/视图中显示这些数据感到困惑。通常我使用{variable name}}来显示传递到视图中的数据。我应该使用什么格式。我尝试使用{{locations.coordinates[0]}},但这给了我一个错误
请帮忙。这是我第一次尝试在Node中执行类似操作。您可以像这样访问经度:
{{locations.loc.coordinates[0]}}
我建议您这样定义模式:
{{locations.loc.coordinates[0]}}
位置:{type:[Number],index:'2d'},//[,]谢谢Zeeshan的回复,我尝试了它,但是我得到了以下错误:错误:第22行的解析错误:…de:{{locations.loc.coordinates[0]}------------------------------------^需要'ID',得到'INVALID',您尝试过它打印的内容吗?{{locations.loc.coordinates}}是的,我做了,它显示了以下错误:错误:第22行的分析错误:…de:{{locations.loc.coordinates[0]}--------------------------^预期的是'ID',得到的是'INVALID',这样做:
router.get('/userlocation/:member_ID',ensurereauthenticated,(req,res)=>{Location.findOne({member\u id:req.params.member\u id}))
在视图文件中,您可以像这样访问它{{locations[0]}}只是为了我的清楚,如果我在机械地传递坐标,它们不会被硬编码吗?