gpt4 book ai didi

javascript - firebase 数据库调用的数组记录了正确的数据,但长度显示为 0

转载 作者:行者123 更新时间:2023-12-02 23:33:04 27 4
gpt4 key购买 nike

我正在调用数据库并从数组中获取每个 map 项并将它们存储在我返回的数组中。如果我登录到控制台,该数组会显示正确的数据,但当我尝试引用任何数据时,该数组似乎为空,并且数组的长度也为零。

$(document).ready(function(){
//This ('click','li') binds all dynamically added shit to respond to click.
$('ul.hiddentext').on('click','li', function(e){
$(this).siblings().css('font-weight','normal');
$(this).css('font-weight','bolder');
let text = $(this).text();
currentWorkoutSelected = text;
var exercises = pullWorkoutTableData(text)
console.log(exercises.length);
var workout = new workoutTemplate(text, user, exercises);
workout.populateTable();

//Populate the title of the workout based on if its already there or not.
if($('h4#workoutTitle').length == 0){
$("table.exerciseList").prepend('<h4 id="workoutTitle">'+ text+ '</h4>');
}
else{
$('h4#workoutTitle').replaceWith('<h4 id="workoutTitle">'+ text+ '</h4>');
};
});
});

//Taking the data needed for the workout table from the database.
function pullWorkoutTableData(workoutName){
var exerciseList = [];
db.collection('workouts').get().then((snapshot)=>{
snapshot.docs.forEach(doc => {
if(doc.data().name === workoutName && doc.data().user.includes(user)){
doc.data().exercises.forEach(element =>{
var exercise = [element.name, element.weight, element.reps];
exerciseList.push(exercise);
});
};
});
});
console.log(exerciseList);
this.setState({exerciseList});
return exerciseList;
};

期望能够调用 populateTable 函数,但由于数组为空,因此未正确创建对象。

最佳答案

问题是 db.collection 返回一个 promise ,并且当您记录它时数据还不可用。

您可以在这里尝试一些操作:

如果您只关心 React 状态,请将设置的状态移至监听器中

function pullWorkoutTableData(workoutName){
var exerciseList = [];
db.collection('workouts').get().then((snapshot)=>{
snapshot.docs.forEach(doc => {
if(doc.data().name === workoutName && doc.data().user.includes(user)){
doc.data().exercises.forEach(element =>{
var exercise = [element.name, element.weight, element.reps];
exerciseList.push(exercise);
});
};
});
this.setState({exerciseList}); // <---- HERE
});

return exerciseList;
};

您可以做的另一件事是使用 async/await 函数,但更新状态可能足以满足您的情况:)

如果您的浏览器支持它。

function pullWorkoutTableData(workoutName){
var exerciseList = [];
return db.collection('workouts').get().then((snapshot)=>{
snapshot.docs.forEach(doc => {
if(doc.data().name === workoutName && doc.data().user.includes(user)){
doc.data().exercises.forEach(element =>{
var exercise = [element.name, element.weight, element.reps];
exerciseList.push(exercise);
});
};
});
return Promise.resolve({ exerciseList })
});
};
$(document).ready(async function(){
//This ('click','li') binds all dynamically added shit to respond to click.
$('ul.hiddentext').on('click','li', function(e){
$(this).siblings().css('font-weight','normal');
$(this).css('font-weight','bolder');
let text = $(this).text();
currentWorkoutSelected = text;
var { exerciseList: exercises } = await pullWorkoutTableData(text)
console.log(exercises.length);
var workout = new workoutTemplate(text, user, exercises);
workout.populateTable();

//Populate the title of the workout based on if its already there or not.
if($('h4#workoutTitle').length == 0){
$("table.exerciseList").prepend('<h4 id="workoutTitle">'+ text+ '</h4>');
}
else{
$('h4#workoutTitle').replaceWith('<h4 id="workoutTitle">'+ text+ '</h4>');
};
});
});

关于javascript - firebase 数据库调用的数组记录了正确的数据,但长度显示为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56406665/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com