我收到了以下switch
:
case "contacts":
html = contacts(required_fields[h]);
break;
contacts
函数在哪里:
function contacts(data) {
return getContacts(function (response) {
response = JSON.parse(response);
var contacts = '';
for (var c = 0; c < response.length; c++) {
contacts += '<input id="id_' + data.id + '" name="feature_' + data.id + '" value="' + response[c].phone_number
+ '" class="form-control contacts required" placeholder="Телефон" type="text" required>';
}
return contacts;
});
}
结果我在行中获得undefined
返回值:
html = contacts(...)
function getContacts(callback) {
$.get("/contacts/" + $('#account').val(), function (data) {
callback(data);
});
}
答案 0 :(得分:2)
你需要在js中考虑异步等待稍后设置变量html的值你需要使用像async / await或promise这样的东西。
function contacts(data) {
return getContacts(function (response) {
return new Promise((resolve,reject) => {
response = JSON.parse(response);
var contacts = '';
for (var c = 0; c < response.length; c++) {
contacts += '<input id="id_' + data.id + '" name="feature_' + data.id + '" value="' + response[c].phone_number
+ '" class="form-control contacts required" placeholder="Телефон" type="text" required>';
}
resolve(contacts);
})
});
}
在你的开关中
html = await contacts(required_fields[h]);
交换机需要异步的功能,如
async function ....(){}