我有这个angularJs更新查询:
$http.post('http://localhost:3000/updateClub?club='+club)
Node.JS可以看到某些内容但无法更新,这是节点jsconsole:
Example app listening on port 3000!
(node:3756) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
express deprecated req.param(name): Use req.params, req.body, or req.query instead app.js:118:20
{ club: '[object Object]' }
undefined
这是我的node.js代码:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
const bodyParser = require('body-parser')
// create application/json parser
const jsonParser = bodyParser.json()
app.post('/updateClub', jsonParser, function (req, res) {
console.log(req.query);
console.log(req.query.categorie); //Fails
var myClub = req.query;
MongoClient.connect('mongodb://localhost:27017', (err, client) => {
var db = client.db('myDb');
db.collection('clubs').updateOne({"_id":myClub._id},{$set:myClub});
});
})
这是我的俱乐部对象,请注意,其中包含一个数组以及一个对象(还有一个> prototype <部分,我不知道如何删除它):
Object { _id: "5d07e982a7d75a67d6383d6b", nom: "Angers Sp", identifiant: 4, date_creation: Date 1919-01-01T00:00:00.000Z, categorie: "professionel", structure: "Ligue 1", affectations: [], img: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYAAAAILCAYAAADhWEs5AAAABmJLR0QA/wD/AP+gvaeTAAAgAElEQVR4nOydd3hURffHv2mb3nsICQkhNNEAoQVBBfnRpFhAioIgwouAovQirwoiSEelKaBIF0SkCUhRREpCkRICoaU3kuym9/n9sWTfBLbembt3s5nP88yT7O6dmXPn7s6ZOXPmDMDhcDgcDofD4XA4HA6Hw+FwOBwOh8PhcDgcs8FCagE49RoXAE4AnB//da/xv/PjZAfA3oC/T5ZvVeO1BQA3HTKVASjU8FkBgPLH/8sBEACVAPIev1cCoLhGGfmP38t/nLfk8bWFj/9XPP5f/jiV6JCNw2EKVwAcWuwAeALwqPHXA4DXE+9V/3WFsoPX1RHXR4rxP2UgB5Cr5nUWgEePU/rj10VSCMup+3AFwFGHDIAPAH8Avo//D3j81xeAX43PXSWSkfM/ivA/hfAI/1MSKY/fSwKQ+jjxWQZHBVcA9Q8ZlB13QwBBAAIfpyAADR7/7yeZdByxyYZSESShtnJIBvAQwAMoZyKcegBXAOaHNZSde2iNFPI4NYSyc+fPnaONDACJUCqEJxNXEGYE7wjqJvYAwgE0hbJjr9nZB0GpBDgcscgAcOdxin+cqv8vlVAujoFwBWDaNMT/Ovpmj/+GAwgGf3Yc06MKyplDtUK4A+A2gFgoTU0cE4N3IqZBIwDPPE6toOzom0LpLcPhmAMKKBXBDQA3H6cbUK5DcCSCKwDj4gngWQAtoezoWz3+30VKoTgcCcmBUhHEArgG4CqAf8FdW40CVwDiEQKgzePUFsqO319SiTicukEllOajS1AqhOq/cimFMke4AmBDYyg7+ZodvoekEnE45sd9AJehVAYxAC6AKwUquAIwHG8AnQB0ANARyg6f72rlcIwPARAH4CKA8wDOQbm2UCGlUHUJrgC0Yw3gOfyvw4+C0tWSw+GYJo…", adresse: {…}, ville: "Angers", … }
我也在firefox中收到此错误消息,但是当参数为node.js时,它仍然会获取数据:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/updateClub?club=[object%20Object]. (Reason: CORS request did not succeed).
如果有人可以帮助我,我将非常感谢。 其他的node.js查询仅使用参数,而不使用完整的对象。
我的俱乐部收藏很好,并且已经包含25个俱乐部
编辑1: 将我的http查询更改为:
$http.post('http://localhost:3000/updateClub',{data:$.param(club)})
现在,node.js req.body似乎获取了对象(文档):
{ data:
'_id=5d07e982a7d75a67d6383d6b&nom=Angers+Sporting+Club+de+l\'Ouest+(SCO+Angers)&identifiant=4&date_creation=Wed+Jan+01+1919+00%3A00%3A00+GMT%2B0000+(Central+European+Standard+Time)&categorie=professionel&structure=Ligue+1&img=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAAYAAAAILCAYAAADhWEs5AAAABmJLR0QA%2FwD%2FAP%2BgvaeTAAAgAElEQVR4nOydd3hURffHv2mb3nsICQkhNNEAoQVBBfnRpFhAioIgwouAovQirwoiSEelKaBIF0SkCUhRREpCkRICoaU3kuym9%2Fn9sWTfBLbembt3s5nP88yT7O6dmXPn7s6ZOXPmDMDhcDgcDofD4XA4HA6Hw%2BFwOBwOh8PhcDgcs8FCagE49RoXAE4AnB%2F%2Fda%2Fxv%2FPjZAfA3oC%2FT5ZvVeO1BQA3HTKVASjU8FkBgPLH%2F8sBEACVAPIev1cCoLhGGfmP38t%2FnLfk8bWFj%2F9XPP5f%2FjiV6JCNw2EKVwAcWuwAeALwqPHXA4DXE%2B9V%2F3WFsoPX1RHXR4rxP2UgB5Cr5nUWgEePU%2Frj10VSCMup%2B3AFwFGHDIAPAH8Avo%2F%2FD3j81xeAX43PXSWSkfM%2FivA%2FhfAI%2F1MSKY%2FfSwKQ%2BjjxWQZHBVcA9Q8ZlB13QwBBAAIfpyAADR7%2F7
不再有cors警告
现在这是我最后的node.js代码:
app.post('/updateClub', jsonParser, function (req, res) {
console.log(req.body); // Can see the object in the console !
var myClub = req.body;
MongoClient.connect('mongodb://localhost:27017', (err, client) => {
var db = client.db('myDb');
db.collection('clubs').updateOne({"_id":myClub._id},{$set:myClub}); // Fails
});
})
它仍然不会更新mongodb上的对象
编辑2:
谢谢乔治,将我的前端查询更改为:
$http.post('http://localhost:3000/updateClub',club)
现在,这是节点控制台显示的内容,它是一个结构良好的对象(对不起,无法显示全部内容,还有烦人的base64图片):
{ _id: '5d07e982a7d75a67d6383d6b',
nom: 'Angers Sporting Club',
identifiant: 4,
date_creation: '1919-01-01T00:00:00.000Z',
categorie: 'professionel',
structure: 'Ligue 1',
affectations: [],
img: ...
这是我最新的node.js代码:
app.post('/updateClub', jsonParser, function (req, res) {
console.log(req.body); // Can see the well formed object in the console !
var myClub = req.body;
MongoClient.connect('mongodb://localhost:27017', (err, client) => {
var db = client.db('myDb');
db.collection('clubs').updateOne({"_id":myClub._id},{$set:myClub}); // There must be a mispelling, i need to replace the whole object (document)
});
})
编辑3:尝试使用replaceOne()
app.post('/updateClub', jsonParser, function (req, res) {
console.log(req.body); // Can see the object in the console !
var myClub = req.body;
MongoClient.connect('mongodb://localhost:27017', (err, client) => {
if (err) throw err;
var db = client.db('myDb');
db.collection('clubs').replaceOne({"_id":myClub._id},myClub); // Fails silently
});
})
注意:尝试了很多事情,insert()可以工作,但是replaceOne()不能!
编辑4
将我的node.js替换为:
try {
db.collection('clubs').replaceOne({_id:"5d07e982a7d75a67d6383d6b"},myClub)
} catch (e){
print(e);
}
这仍然没有更新!可能是因为“ myClub”文档太大了吗? console.log(myClub._id)正在运行,但是replaceOne()静默失败!
console.log(typeof(myClub))提供对象,所以应该没问题!
编辑6: ANGULARJS对象与node.js收到的对象不同!
我发现了错误,Node.js解析的JSON包含[Object]而不是嵌套的对象数据:
adresse:
{ address_components:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ],
adr_address:
如何摆脱这种情况并获取真实对象数据? (AngularJs发送的对象包含所有“对象”数据,然后node.js用[object]替换它,为什么,请帮忙?
编辑7: 将我的前端更改为:
$http.post('http://localhost:3000/updateClub',club, {headers: {'Content-Type': 'application/json'} })
将我的node.js代码更改为此:
const jsonParser = bodyParser.json()
app.use(bodyParser.json({
inflate: true,
strict:false
}));
我的嵌套JSON对象仍然没有解析,相反,有[object]
我应该怎么做才能获取嵌套对象的内容?
编辑8
我从官员那里得到了答案:
默认情况下,Node.js console.log仅显示深度为2:
如此
console.dir(req.body,{depth:Infinity})正确显示了整个JSON文档。
所以我真的不知道为什么replaceOne()不起作用,我发白了,我没有解释。
编辑9:已解决
我最终得到了这段代码,但是我不认为这是最好的做法,我添加了第二个标识符,该标识符来自我的旧bdd,因此我将其用作过滤器,并删除了mongoDb生成的_id,该标识符是生成错误,则替换工作正常:
app.post('/updateClub', function (req, res) {
myClub = req.body;
delete myClub._id;
MongoClient.connect('mongodb://localhost:27017', { useNewUrlParser: true }, (err, client) => {
if (err) throw err;
var db = client.db('myDb');
try {
db.collection('clubs').replaceOne({"identifiant":myClub.identifiant},myClub)
} catch (e){
print(e);
}
})
})
编辑:尝试从myClub._id中删除引号,并尝试提供的第一个没有运气的代码 myClub._id = myClub._id.replace(/ [“'] / g,”“);
这是myClub._id:5d07e982a7d75a67d6383d6c,看起来不错
最终:
好的,我在那里找到了一个解释:
好吧,我需要使用_id才能替换一个,这更方便。