我正在尝试获取,操作和保存猫鼬文档,但得到错误的结果。请参见下面的代码段:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Ofensas</title>
<!-- Bootstrap -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<p><br/><br></p>
<div class="container">
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Descrição</th>
<th>Número</th>
<th>ID</th>
</tr>
</thead>
</table>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" ></script>
<script>
$.getJSON("exemplo.json",function(data){
var items = [];
$.each(data, function(key, val){
items.push("<tr>");
items.push("<td name='" + key + "'>" + val.offense_name +"</td>");
items.push("<td nuemro='" + key + "'>" + val.count + "</td>");
items.push("<td name='" + key + "'>" + val.offense_id + "</td>");
items.push("</tr>");
});
$("<tbody/>", {html: items.join("")}).appendTo("table");
});
</script>
</body>
</html>
每次我遍历x = [{id:"abc", value:10, name:"andy"}, {id:"abc", value:20, name:"mark"}];
x.forEach(function(x){
var amount = x.value;
y.findOne({id:x.id}).populate("name").exec(function(err, y){
y.value += amount;
y.save();
});
});
并尝试修改forEach
时,都会得到错误的结果。
y.value
如果能在这里帮助我,将不胜感激。
答案 0 :(得分:0)
没有
在对象x中id字段
。 只是
x.value
和
x.name
有效以进行检索。 因此,您的查询需要更改。
通常在MongoDB中,您也只使用内部_id属性来标识对象。 您必须注意常规的foreach循环和异步操作。最好使用类似
的库异步
或异步/等待