[英]Inserting into MongoDB using Node js
Inserting into Mongo DB using the mongojs module fails cryptically,I have two functions,setupMongoDB and pushRequestsToMongoDB(what they do is self-explanatory). 使用mongojs模块插入Mongo DB会因加密失败而失败,我有两个功能,setupMongoDB和pushRequestsToMongoDB(它们的作用不言而喻)。 I get a request from my web-page and parse the data to JSON.
我从网页收到请求,然后将数据解析为JSON。
The console looks like this: 控制台看起来像这样:
created worker: 22987
created worker: 22989
created worker: 22990
created worker: 22991
{ type: 'line',geometry: '`tvtBat~_Wnaoi@_kc~BzlxZbrvdA{fw[`mw}@' }
object
[Error: connection closed]
The code that did produces the error looks like this: 确实产生错误的代码如下所示:
var mongo=require('mongojs');
var collections=['testData'];
var dbURL='localhost:3000/mapData';
var db=mongo.connect(dbURL,collections);
var insert=function(obj)
{
db.testData.save(obj,function(err,obj){
if(err || !obj)
{
console.log(err);
}
else
{
console.log('Data successfully inserted with _id '+obj['_id']);
}
});
};
exports.insert=insert;
This is how I use the function: 这就是我使用函数的方式:
var express=require('express');
var app=express();
var mongo=require('./mongo_try');
app.use(express.bodyParser());
app.post('/map',function(req,res){
var data=req.body;
console.log(data);
console.log(typeof data);
mongo.insert(data);
});
I'm very confused at what "conn.markers.save" intends to do. 我对“ conn.markers.save”打算做什么感到非常困惑。 Is this a mongoose call?
这是猫鼬电话吗? Mongodb node native doesn't have a "save" command.
Mongodb节点本机没有“保存”命令。 Do you mean to get the "markers" collection, and then insert the data?
您是要获取“标记”集合,然后插入数据吗? You won't need to stringify it.
您无需将其分类。
Are you using this: https://github.com/mafintosh/mongojs ? 您是否在使用: https : //github.com/mafintosh/mongojs ?
You shouldn't have to stringify that save command either. 您也不必使该保存命令字符串化。 Change this line:
更改此行:
conn.markers.save(obj,function(err,data){
Or if the contents of "obj" are already a string, change it to: 或者,如果“ obj”的内容已经是字符串,请将其更改为:
conn.markers.save(JSON.parse(obj),function(err,data){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.