MongoError: key $get不能以'$'开头

MongoError: key $get must not start with '$'

本文关键字:开头 不能 key get MongoError      更新时间:2023-09-26

我使用node js和express js框架在服务器端插入mongodb中的对象,我使用angular js。但是它抛出的错误是MongoError: key $get不能以'$'

开头

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不能以'$'开头

提前感谢

您真的想要存储控制台显示的相同联系人吗?使用所有的$命令?还是说它被附加了不必要的东西?你从你的前端发送什么对象?

作为mongoDB $的答案是一个保留关键字,它不应该在你试图保存的文档的关键字内使用。如果他们允许$ in键,查询将开始失败,因为mongo使用特殊目的的$关键字,这将是类似于SQL注入的安全威胁。

作为一个解决方案,如果你真的想用相同的数据存储联系人,我建议用get或_get替换$get,类似地,用$ keyword替换所有的键。

谢谢