我正在尝试使用缺少2个值的JSON对象对数据库进行更新,因此我必须手动添加它们。其中一个“ include”假定值没有问题,因为该值始终为“ yes”。但是对于“推荐重量”,我必须调用另一个从数据库获取其值的函数。
这样做时,我调用的函数似乎正常运行,因为对返回的console.log()进行显示会显示所需的值。但是,在将结果存储到该属性的console.log()时,它始终显示未定义。
$scope.editDataAdd = function(data) {
var oldVariable = data.variable;
//console.log(getRecWeight(data.variable)); //shows undefined
data.recommendedweight = getRecWeight(data.variable);
console.log(data.recommendedweight); //shows undefined
data.include = "yes";
delete data._id;
delete data.oldVariable;
$http
.put("../somewhere/" + data.variable, data)
.then(function(response) {
console.log("Data " + data.variable + " edited!");
M.toast({html: '<i class="material-icons">done</i> ' + oldVariable + ' has been edited succesfully!'},4000);
refresh();
}, function(response) {
M.toast({html: '<i class="material-icons">error_outline</i> Error editing data!'},4000);
refresh();
});
};
//TODO
var getRecWeight = $scope.getRecWeight = function(variable){
$http
.get("../somewhere/" + variable)
.then(function(response){
console.log(response.data[0].recommendedweight); //shows 10 as expected
return response.data[0].recommendedweight;
});
};
我尝试了几件事,但还是没有运气。也许你可以帮我一下。
谢谢。