我正在使用nodejs(expres framwork)和使用mongoose的mongodb。我正在尝试将地理位置坐标保存到我的数据库中。
这就是我的架构的样子:
//defining a schema
var TopicSchema = mongoose.Schema({
topicTitle: String,
topicDescription: String,
topicDateCreated: {
type: Date,
default: Date.now
},
fbId: String,
twId: String,
location: {
type: {
type : String
},
coordinates: [Number]
}
});
TopicSchema.index({ location: '2dsphere' });
var Topic = restful.model('topic', TopicSchema);
Topic.methods(['get', 'put', 'post', 'delete']);
Topic.register(app, '/data');
通过socket.io我将数据传递给服务器。服务器接收数据但保存后我收到以下错误:
MongoError: Can't extract geo keys
答案 0 :(得分:1)
错误在于:
let dictionary = NSDictionary(objects: [user.username, image!], forKeys: ["username", "image"])
这里发生的是你将两个数字组合成一个字符串,var coordinates = LON + ", " + LAT;
成为一个字符串。
您将架构定义为具有coordinates
的位置点数组。将[Number]
作为字符串发送将违反您的架构定义。
要解决您的错误,请创建一个地理点数组,例如coordinates
答案 1 :(得分:0)
Mongodb使用经度 - 纬度系统来存储地理位置索引。此坐标是浮点值,但您可以将它们设置为要使用的数字。请尝试以下方案:
var TopicSchema = mongoose.Schema({
topicTitle: String,
topicDescription: String,
topicDateCreated: {
type: Date,
default: Date.now
},
fbId: String,
twId: String,
location: {
type: {
type : String
},
coordinates: [ ]
}
});
中看到其他示例
答案 2 :(得分:-1)
$("#btnPostTopic").on("click", function(e)
{
//get the value of the inputfields and textarea
var title = $('#TopicTitle').val();
var description = $('#TopicDescription').val();
var LON = parseFloat($('#longitude').val());
var LAT = parseFloat($('#latitude').val());
var coordinates = LON + ", " + LAT;
console.log("COORDINATES are " + coordinates);
var resultLocalStorageFB = document.getElementById("resultFB");
var resultLocalStorageTW = document.getElementById("resultTW");
resultLocalStorageFB.value = localStorage.getItem("useridfb");
resultLocalStorageTW.value = localStorage.getItem("useridtw");
console.log(resultLocalStorageFB);
console.log(resultLocalStorageTW);
var FbId = $('#resultFB').val();
var TWId = $('#resultTW').val();
console.log('Fb Id: ' + FbId);
console.log('TW Id: ' + TWId);
var Point = "Point";
result = {
"topicTitle": title,
"topicDescription": description,
"fbId": FbId,
"twId": TWId,
"location": {
"type": Point,
coordinates
}
};
console.log("Values of inputfield & textarea: " + result);
//makes the connection to the server and send the data
var socket = io.connect('http://localhost:3001');
socket.emit('sendTopic', result);
//console.log("Gets send: " + result);
});
});