Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从firebase获取数据并存储到数组中时发生Typescript错误[IONIC]_Javascript_Reactjs_Typescript_Variables_Ionic Framework - Fatal编程技术网

Javascript 从firebase获取数据并存储到数组中时发生Typescript错误[IONIC]

Javascript 从firebase获取数据并存储到数组中时发生Typescript错误[IONIC],javascript,reactjs,typescript,variables,ionic-framework,Javascript,Reactjs,Typescript,Variables,Ionic Framework,我想有一个函数,可以获取firebase实时数据库中的所有数据,并将其放入一个数组中,以便映射到react中。我收到一个打字脚本错误,我不知道如何修复它 这就是错误:“any[]”类型的参数不能分配给“SetStateAction”类型的参数。 类型“any[]”不可分配给类型“never[]”。 类型“any”不可分配给类型“never”。ts(2345) 它发生在setProducts(Produkte)上 以及相关代码: const loadData = async (dbRefName:

我想有一个函数,可以获取firebase实时数据库中的所有数据,并将其放入一个数组中,以便映射到react中。我收到一个打字脚本错误,我不知道如何修复它

这就是错误:“any[]”类型的参数不能分配给“SetStateAction”类型的参数。 类型“any[]”不可分配给类型“never[]”。 类型“any”不可分配给类型“never”。ts(2345) 它发生在setProducts(Produkte)上

以及相关代码:

const loadData = async (dbRefName: string) => {
let Produkte: any[] = [];
var dbProduktRef = firebase.database().ref(dbRefName);  
dbProduktRef.on("value", (snapshot) => {
  console.log("FireB ", snapshot.val());
  let key;
  snapshot.forEach((child) => {
    key = child.key || '{}';
    const snapVal =snapshot.val();
    Produkte.push(snapVal[key]);
  });
  setProducts(Produkte)
});  };

看看下面我的代码注释,也许它对您的调试思维过程很有用

const loadData = async (dbRefName: string) => {
        let Produkte: any[] = [];
        var dbProduktRef = firebase.database().ref(dbRefName);
        dbProduktRef.on("value", (snapshot) => {
            console.log("FireB ", snapshot.val());
            
            let key;
            snapshot.forEach((child) => {
    
                key = child.key || '{}'; // [a] This doesn't seem right. What's your intention here?
                const snapVal = snapshot.val(); // [b] Why is this? Since you are alreadying looping through snapshot
                Produkte.push(snapVal[key]); // [c] This is error prone due to [a]. See the above code comment
            });
    
            // [d] I am assuming this is a react hook or some kind of function that updates a state, no?
            setProducts(Produkte); // [e] What is the expected data type of the setProduct paramater?
        });

};