尝试在此 trello.get 代码中获得正确的 JavaScript 变量范围
Trying to get the JavaScript variable scope correct in this trello.get code
我一直在努力在这段代码中正确设置变量 'listName' 范围 ...
var loadedAtriskopps = function(opps) {
var listName = 'empty';
$.each(opps.cards, function(index, opp) {
Trello.get(
'/cards/' + opp.id + '/list?fields=name', {fields: "id, name"}, function(list, err) {
var listName = list.name;
console.log(listName); //this returns the correct value
});
console.log(listName); //this returns 'empty'
var opp = $("<div class='row'><div class='col-xs-6'><p class='output'>" + opp.name + "</b><br>On List: " + listName + " </p></div> </div>");
$('#opps').append(opp)
});
};
在 Trello.get 函数内部它 returns 正确,但在它外部则不然。经过数小时的研究,仍然无法解决问题。
更新:
根据建议,尝试了此代码:
var loadedAtriskopps = function(opps) {
var listName = 'empty';
$.each(opps.cards, function(index, opp) {
console.log(opp.name); // returns correct value
Trello.get('/cards/' + opp.id + '/list?fields=name', {fields: "id, name"})
.success(function(list) {
listName = list.name;
console.log(listName); // // returns correct value
// next line errors "Uncaught TypeError: Cannot read property 'name' of undefined" but listName is correct
var opp = $("<div class='row'><div class='col-xs-6'><p class='output'>" + opp.name + "</b><br>On List: " + listName + " </p></div> </div>");
$('#opps').append(opp);
})
.error(function(err) {
console.log("An error: " + err);
});
});
};
似乎我在成功回调中丢失了 opp
对象的定义。
解决方案:
var loadedAtriskopps = function(opps) {
var listName = 'empty';
$.each(opps.cards, function(index, opp) {
Trello.get('/cards/' + opp.id + '/list?fields=name', {fields: "id, name"})
.success(function(list) {
listName = list.name;
var card = $("<div class='row'><div class='col-xs-6'><p class='output'>" + opp.name + "</b><br>On List: " + listName + " </p></div> </div>");
$('#opps').append(card);
})
.error(function(err) {
console.log("An error: " + err);
});
});
};
谢谢达伦和安迪!
我认为您需要将 post-ajax 操作移至成功回调中。这应该确保您在 之后具有实际值 ,ajax 具有 listName
的值。
var loadedAtriskopps = function(opps) {
var listName = 'empty';
$.each(opps.cards, function(index, opp) {
Trello.get('/cards/' + opp.id + '/list?fields=name', {fields: "id, name"})
.success(function(list) {
listName = list.name;
console.log(listName); // should return correct value
var _opp = $("<div class='row'><div class='col-xs-6'><p class='output'>" + opp.name + "</b><br>On List: " + listName + " </p></div> </div>");
$('#opps').append(_opp);
})
.error(function(err) {
console.log("An error: " + err);
});
});
};
我一直在努力在这段代码中正确设置变量 'listName' 范围 ...
var loadedAtriskopps = function(opps) {
var listName = 'empty';
$.each(opps.cards, function(index, opp) {
Trello.get(
'/cards/' + opp.id + '/list?fields=name', {fields: "id, name"}, function(list, err) {
var listName = list.name;
console.log(listName); //this returns the correct value
});
console.log(listName); //this returns 'empty'
var opp = $("<div class='row'><div class='col-xs-6'><p class='output'>" + opp.name + "</b><br>On List: " + listName + " </p></div> </div>");
$('#opps').append(opp)
});
};
在 Trello.get 函数内部它 returns 正确,但在它外部则不然。经过数小时的研究,仍然无法解决问题。
更新:
根据建议,尝试了此代码:
var loadedAtriskopps = function(opps) {
var listName = 'empty';
$.each(opps.cards, function(index, opp) {
console.log(opp.name); // returns correct value
Trello.get('/cards/' + opp.id + '/list?fields=name', {fields: "id, name"})
.success(function(list) {
listName = list.name;
console.log(listName); // // returns correct value
// next line errors "Uncaught TypeError: Cannot read property 'name' of undefined" but listName is correct
var opp = $("<div class='row'><div class='col-xs-6'><p class='output'>" + opp.name + "</b><br>On List: " + listName + " </p></div> </div>");
$('#opps').append(opp);
})
.error(function(err) {
console.log("An error: " + err);
});
});
};
似乎我在成功回调中丢失了 opp
对象的定义。
解决方案:
var loadedAtriskopps = function(opps) {
var listName = 'empty';
$.each(opps.cards, function(index, opp) {
Trello.get('/cards/' + opp.id + '/list?fields=name', {fields: "id, name"})
.success(function(list) {
listName = list.name;
var card = $("<div class='row'><div class='col-xs-6'><p class='output'>" + opp.name + "</b><br>On List: " + listName + " </p></div> </div>");
$('#opps').append(card);
})
.error(function(err) {
console.log("An error: " + err);
});
});
};
谢谢达伦和安迪!
我认为您需要将 post-ajax 操作移至成功回调中。这应该确保您在 之后具有实际值 ,ajax 具有 listName
的值。
var loadedAtriskopps = function(opps) {
var listName = 'empty';
$.each(opps.cards, function(index, opp) {
Trello.get('/cards/' + opp.id + '/list?fields=name', {fields: "id, name"})
.success(function(list) {
listName = list.name;
console.log(listName); // should return correct value
var _opp = $("<div class='row'><div class='col-xs-6'><p class='output'>" + opp.name + "</b><br>On List: " + listName + " </p></div> </div>");
$('#opps').append(_opp);
})
.error(function(err) {
console.log("An error: " + err);
});
});
};