预期的位置对象,位置数组的格式不正确

编程入门 行业动态 更新时间:2024-10-28 14:34:47
本文介绍了预期的位置对象,位置数组的格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经花了这么直接的时间做.我只想使用nodejs,mongoose,restify堆栈对用户模型执行CRUD操作.我的mongo实例在mongolab上. 用户应包含一个"loc"字段.用户架构如下:

I have spent doing such a straight forward thing. I just want to do a CRUD operation on a user model using nodejs, mongoose, restify stack. My mongo instance is on mongolab. The user should contain a "loc" field . User schema is as follows :

var mongoose = require('mongoose') var Schema = mongoose.Schema; var userSchema = new Schema( { email_id : { type: String, unique: true }, password: { type: String}, first_name: String, last_name: String, age: String, phone_number: String, profile_picture: String, loc: { type: {}, coordinates: [Number] } }); userSchema.index({loc:'2d'}); var User = mongoose.model('user', userSchema); module.exports = User;

用于发布的其余api如下:

the rest api used to post is as follows :

create_user : function (req, res, next) { var coords = []; coords[0] = req.query.longitude; coords[1] = req.query.latitude; var user = new User( { email_id : req.params.email_id, password: req.params.password, first_name: req.params.first_name, last_name: req.params.last_name, age: req.params.age, phone_number: req.params.phone_number, profile_picture: req.params.profile_picture, loc: { type:"Point", coordinates: [1.0,2.0] // hardcoded just for demo } } ); user.save(function(err){ if (err) { res.send({'error' : err}); } res.send(user); }); return next(); },

现在,当我在curl -X POST localhost:3000/user --data "email_id=sdass@dfAadsfds&last_name=dass&age=28&phone_number=123456789&profile_picture=www.jakljf&longitude=1.0&latitude=2.0"上执行POST呼叫时 我收到以下错误

Now when i do a POST call on curl -X POST localhost:3000/user --data "email_id=sdass@dfAadsfds&last_name=dass&age=28&phone_number=123456789&profile_picture=www.jakljf&longitude=1.0&latitude=2.0" I get the following error

{ error: { code: 16804 index: 0 errmsg: "insertDocument :: caused by :: 16804 location object expected, location array not in correct format" op: { email_id: "sdass@dfAadsfdsadkjhfasvadsS" password: "sdass123DadakjhdfsfadfSF45" first_name: "shaun" last_name: "dass" age: "28" phone_number: "123456789" profile_picture: "www.jakljf" loc: { coordinates: [2] 0: 1 1: 2 - type: "Point" }- _id: "55efc95e0e4556191cd36e5e" __v: 0 }- }- }

如果我从模型中删除loc字段,则位置字段会出现问题,因为POST调用可以正常工作

The location field is giving problems as the POST call works just fine if i remove the loc field from model

以下是我的点击次数/试用次数: 1)将userSchema.index({loc:'2d'});更改为userSchema.index({loc:'2dsphere'}); 2)将loc模式更改为Stackoverflow中提供的所有内容.我想知道定义这个的正确方法. 3)传递硬编码的2d数组,但仍然说Location object expected, location array not in correct format"为此需要什么格式?

Below are the hits/trials I did : 1) Change userSchema.index({loc:'2d'}); to userSchema.index({loc:'2dsphere'}); 2) Changing loc schema to everything given in Stackoverflow. I would like to know the right way to define this though. 3) Passing the hardcode 2d array but still it says Location object expected, location array not in correct format" what format is required for this ?

在这方面的任何帮助都将不胜感激.谢谢.

Any help in this regard is greatly appreciated. Thanks.

推荐答案

MongoDB 2d索引需要旧版坐标对格式,它只是一个像[1, 2]这样的坐标数组.

MongoDB 2d index requires the legacy coordinates pairs format, which is just an array of coordinates like [1, 2].

如果需要GeoJSON支持,请使用 2dsphere索引.

If you need GeoJSON support, please use the 2dsphere index.

userSchema.index({loc:'2dsphere'});

更多推荐

预期的位置对象,位置数组的格式不正确

本文发布于:2023-10-08 02:14:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471218.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   数组   不正确   对象   格式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!