[英]Cant access object that have been declare on javascript
I'm trying to get some value over my server using jquery's $.get method and store the result in an object.我正在尝试使用 jquery 的 $.get 方法在我的服务器上获得一些价值,并将结果存储在 object 中。
What I'm doing is running my function inside a loop:我正在做的是在循环中运行我的 function :
function renderTabelScheduledEmploye(employees) {
$('#containerDataStaff').empty();
let record = '';
let count = 1;
if (employees.length > 0) {
$.each(employees, function(key, value) {
getFromServerScheduleCount(employees[key].schedule_employee_id);
console.log(scheduleCountData);
record += '<tr>';
record += '<td>' + count + '</td>';
record += '<td>( ' + employees[key].emp_kode + ') - ' + employees[key].emp_full_name + '</td>';
record += '<td>' + scheduleCountData[0].work + '</td>';
record += '<td>' + scheduleCountData[0].offDay + '</td>';
record += '<td>' + scheduleCountData[0].leave + '</td>';
record += '<td>' + scheduleCountData[0].shiftCount + '</td>';
record += '<td><a href="#" data-id="' + employees[key].schedule_employee_id + '" class="btnSettingShift">Set Shift</a></td>';
record += '</tr>';
count++;
});
}
$('#containerDataStaff').append(record);
}
Then I create a function to retrieve JSON data from my server using the $.get function, then store the data result to an object that has been declared: Then I create a function to retrieve JSON data from my server using the $.get function, then store the data result to an object that has been declared:
var scheduleCountData = new Object();
function getFromServerScheduleCount(schedule_employee_id) {
let url = `{{ url('/departement/schedule/manage/schedule/count') }}`;
url = url + '/' + schedule_employee_id + '/get';
let data = {};
$.get(url, function(data) {
let obj = JSON.parse(data);
data.work = obj.work;
data.dayOff = obj.dayOff;
data.leave = obj.leave;
data.shifts = obj.shifts;
scheduleCountData.new = data;
})
}
I can see in the console that the object has filled with the data我可以在控制台中看到 object 已填充数据
[object success to fill][1]
But when i try to access in code like this但是当我尝试访问这样的代码时
scheduleCountData[0]
and log the result to to the console, it shows undefined
.并将结果记录到控制台,它显示
undefined
。
This little bit frustrating for me.这对我来说有点令人沮丧。 I hope someone can help me.
我希望有一个人可以帮助我。 Thanks for community.
感谢社区。
EDIT: Sorry i miss i try to call that object with code like this编辑:对不起,我想念我尝试用这样的代码调用 object
scheduleCountData.new
but still undefined on console但在控制台上仍未定义
Seems youre not waiting for the load to finish before trying to use the data:在尝试使用数据之前,您似乎没有等待加载完成:
// This has a get call, which is async (takes time to load)
getFromServerScheduleCount(employees[key].schedule_employee_id);
// The script will continue, without waiting for the load to finish, so can be empty
console.log(scheduleCountData);
I would suggest adding a callback, like this:我建议添加一个回调,如下所示:
getFromServerScheduleCount(employees[key].schedule_employee_id, function(scheduleCountData) {
console.log(scheduleCountData);
});
function getFromServerScheduleCount(schedule_employee_id, callback) {
$.get(url, function(data) {
// ...
callback(data);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.