我使用 Node js 和 Express JS 框架在服务器端将对象插入到 mongo db 中,前端我使用 Angular js。但它抛出的错误是 MongoError: key $get must not start与“$”
server.js
app
.use(bodyParser.json())
.post('/contact', upload.single('file'), function(req,res){
var contact = req.body;
delete contact.$promise;
delete contact.$resolved;
console.log(contact);
//console.log(req.file);
contact.userId = 1;
db.collection(CONTACTS_COLLECTION).insertOne(contact, function(err, doc) {
if (err) {
console.log(err + " Unsuccess");
} else {
console.log(doc.ops[0]);
res.status(201).json(doc.ops[0]);
}
});
})
在控制台显示联系人的值
{ firstName: 'abc,text',
lastName: 'abc,text',
toJSON: 'function (){var a=v({},this);delete a.$promise;delete a.$resolved;return a}',
'$get': 'function (a,b,d){x(a)&&(d=b,b=a,a= {});a=m[c].call(this,a,this,b,d);return a.$promise||a}',
'$save': 'function (a,b,d){x(a)&&(d=b,b=a,a={});a=m[c].call(this,a,this,b,d);return a.$promise||a}',
'$query': 'function (a,b,d){x(a)&&(d=b,b=a,a={});a=m[c].call(this,a,this,b,d);return a.$promise||a}',
'$remove': 'function (a,b,d){x(a)&&(d=b,b=a,a={});a=m[c].call(this,a,this,b,d);return a.$promise||a}',
'$delete': 'function (a,b,d){x(a)&&(d=b,b=a,a={});a=m[c].call(this,a,this,b,d);return a.$promise||a}',
'$update': 'function (a,b,d){x(a)&&(d=b,b=a,a={});a=m[c].call(this,a,this,b,d);return a.$promise||a}' }
错误
MongoError: key $get must not start with '$' Unsuccess
提前谢谢
最佳答案
您真的想存储控制台中显示的相同联系人吗?与所有 $ 命令?或者它是否被意外附加?您从前端发送什么对象?
作为 mongoDB $ 的答案,它是一个保留关键字,不应在您尝试保存的文档的键内使用它。如果他们允许在键中使用 $ ,则查询将开始失败,因为 mongo 使用具有特殊目的的 $ 关键字,这也将是类似于 SQL 注入(inject)的安全威胁。
作为一种解决方案,如果您确实想使用相同的数据存储联系人,我建议将 $get 替换为 get 或 _get,类似地,将所有键替换为 $ 关键字。
谢谢
关于javascript - 蒙戈错误: key $get must not start with '$' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39761253/