的script.js
for(var i = 0;i<$scope.data_acc_lv.length;i++)
{
var input3 = {
"_id": $scope.accLvID + i,
"acc_id":$scope.accLvID,
"name": $scope.data_acc_lv[i].names,
"read": $scope.data_acc_lv[i].read,
"execute": $scope.data_acc_lv[i].execute,
}
$http.post("http://localhost:1234/access_menu",input3)
.success(function(res){
if(res.error == 0)
{
}
});
}
的script.js
app.post('/access_menu',function(req,res){
var IDMenu = req.body._id;
var Forein = req.body.acc_id;
var Name = req.body.name;
var Read = req.body.read;
var Execute = req.body.execute;
var data = {"error":1,"Access_menu":""};
if(!!Name && !!Read && !!Execute)
{
db.collection("access_menu").insert({_id:IDMenu,acc_id:Forein,name:Name,read:Read,execute:Execute},function(err,req){
if(!!err)
{
data['Acess_menu'] = "input error";
}
else
{
data['error'] = 0;
data['Access_menu'] = "input berhasil"
}
res.json(data);
});
data = {"error":1,"Access_menu":""};
}
});
所以我尝试从表到数据库输入数据,但我总是得到最后一个索引的值(所有以前的值都替换为最后一个索引值),所以导致问题的原因。谢谢
答案 0 :(得分:0)
var temp = [];
for(var i = 0;i<$scope.data_acc_lv.length;i++){
var input3 = {
"_id": $scope.accLvID + i,
"acc_id":$scope.accLvID,
"name": $scope.data_acc_lv[i].names,
"read": $scope.data_acc_lv[i].read,
"execute": $scope.data_acc_lv[i].execute,
}
temp.push(input3)
}
$http.post("http://localhost:1234/access_menu",temp )
.success(function(res){
if(res.error == 0)
{
}
});
为什么不只是格式化你的数组然后在帖子上发送整个数组 然后在你的script.js中循环?
答案 1 :(得分:0)
您可以将代码中的代码包装在一个立即调用的函数中
(function( i ) {
var input3 = {
"_id": $scope.accLvID + i,
"acc_id":$scope.accLvID,
"name": $scope.data_acc_lv[i].names,
"read": $scope.data_acc_lv[i].read,
"execute": $scope.data_acc_lv[i].execute,
}
$http.post("http://localhost:1234/access_menu",input3)
.success(function(res){
if(res.error == 0)
{
}
});
})(i);
希望这有帮助。