我正在尝试通过AJAX将变量传递给API。这是角度控制器:
$scope.register = function() {
_.each($scope.photos, function(images) {
$upload.upload({
url: '/api/indorelawan/timaksibaik/register/upload-images',
method: 'POST',
data: {},
file: images
})
.success(function(data) {
$scope.team.photos.push(data.result.path);
})
});
$http({
method : 'POST',
url : '/api/indorelawan/timaksibaik/register',
data : $.param($scope.team),
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
})
.success(function(data) {
if (!data.success) {
...
}
else {
...
}
});
}
我在调用/ register API之前尝试了console.log $ scope.team.photos。它完美地显示数据。但是当运行/ register API时,不包括$ scope.team.photos。这是API:
/*Register Tim Aksi Baik*/
apiRouter.post('/timaksibaik/register', function(req, res) {
// TODO: Create new value to access general statistics data, e.g.: response time.
console.log(req.body);
var team = new GoodActionTeam();
_.each(req.body, function(v, k) {
team[k] = v;
});
team.created = new Date();
team.save(function(err, data) {
if (err) {
res.status(500).json({
success: false,
message: "Gagal menyimpan data organisasi baru.",
system_error: "Error while saving organization data: " + err.message
});
}
else {
res.status(200).json({
success: true,
message: "Organisasi Berhasil Dibuat",
result: data
});
}
});
});
req.body的输出仅为:
{logo:'/ uploads / user_avatar / register / 18 / 1-18:18:18:3.png', 名称:'ererr', url_string:'ererr', 描述:'dfdfd', 焦点:['549789127e6a6e2c691a1fc0','549789127e6a6e2c691a1fc0']}
将数据传递给API时,似乎不包含$ scope.team.photos。出了什么问题?
答案 0 :(得分:2)
$upload.upload()
是异步的,当您使用$scope.team
发帖时,无法保证所有上传成功回调都已完成