小编Ibr*_*nku的帖子

未捕获的类型错误:无法添加属性 0,对象在 Array.push 中不可扩展

从 firebase 获取数据并将数据推送到数组中时出现此错误。在这里,我定义了一个临时数组,当我将 firebase onValue 内的数据推送到此临时数组时,我收到此错误 Uncaught TypeError: Cannot add property 0, object is not extensible at Array.push。这是我的代码

function Room() {
const [textInput, setTextInput] = useState('');
const temp = [];

const handleTextInputChange = (event) => {
    setTextInput(event.target.value);
};

const handleSubmit = () => {
    console.log('here goes');
    if (textInput !== '') {
        const refer = ref(database, 'rooms/');
        push(refer, textInput).then(() => {
            setTextInput('');
            toast.success('Added Successfully!');
        });
    }
};

useEffect(() => {
    const refer = ref(database, 'rooms/');
    onValue(refer, (snap) => {
        snap.forEach((child) => …
Run Code Online (Sandbox Code Playgroud)

javascript firebase reactjs firebase-realtime-database

6
推荐指数
1
解决办法
2万
查看次数