简体   繁体   English

解构 object 反应来自 firebase 的本机钩子返回未定义

[英]Destructuring object react native hooks from firebase returns undefined

This is a video recording app Im working on and after uploading to firebase in an array it then I have it logged on the console.log bellow.这是我正在开发的视频录制应用程序,在上传到数组中的 firebase 之后,我将其登录到下面的 console.log 中。 I am new to React Hooks and all, would really appraciate some help!我是 React Hooks 的新手,真的会得到一些帮助!

I have been unable to destructure this, I need the uri values in separate variable so it can be outputted in a <Video/> component within a <FlatList/> .我无法解构它,我需要单独变量中的 uri 值,以便它可以在<FlatList/>内的<Video/>组件中输出。

Here is the array that needs to be destructured: Note: I changed the key/uri values here for simplification.这是需要解构的数组: 注意:为了简化,我在这里更改了键/uri 值。

 Array [
  Array [
    Object {
      "key": "0001",
      "video": Object {
        "uri": "file:///var/mobile/Containers/Data/Application/...",
      },
    },
    Object {
      "key": "0002",
      "video": Object {
        "uri": "file:///var/mobile/Containers/Data/Application/...",
      },
    },
  ],
]

Here is what I tried... const values = videoArray.uir; console.log("uirs",values);这是我尝试过的... const values = videoArray.uir; console.log("uirs",values); const values = videoArray.uir; console.log("uirs",values);

Here is all the code bellow: Note: I've commented out the Flatlist, but I want the uri to output there...这是下面的所有代码:注意:我已经注释掉了平面列表,但我想要那里的 output 的 uri...



import React, { useState, useEffect } from 'react'
import { FlatList, View, TouchableOpacity, Text, StyleSheet, SafeAreaView } from 'react-native';
import { Center } from '../components/Center'
import { Video } from 'expo-av';
import firebase from '../firebase'
const videoRef = firebase.database().ref('videoCollaction');

export const FeedScreen = ({ }) => {

    var [videoArray, setVideo] = useState([]);

    useEffect(() => {
      const videoArray = []; // temp array
      videoRef.on("value", childSnapshot => {
        childSnapshot.forEach(doc => {
            videoArray.push({
            key: doc.key,
            video: doc.toJSON().video
          });
        });
        setVideo(videoArray); // update state array
      });
    }, []);

       //get uri value from videoArray array...
        const values = videoArray.uir;



    return (
        <SafeAreaView>
            <Text>Feed Screen</Text>
             //log uir only 
             {console.log("uirs",values)}
            {/* array values here */}
            {console.log(" display Array",[videoArray])}

            {/* <FlatList
                data={[videoArray]}
                renderItem={({ item, index }) => {
                    return (
                        <View>

                            <Text style={{ fontSize: 35, color: 'red' }}>Video: {item.uri}</Text>


                            <TouchableOpacity onPress={() => console.log('pressed')}><Text style={{ color: 'blue' }}>Expand</Text></TouchableOpacity>
                        </View>
                    );
                }} keyExtractor={({ item }, index) => index.toString()}>
            </FlatList> */}


            <Video
                source={{ uri: {videoArray} }}
                // shouldPlay={this.state.shouldPlay}
                // isMuted={this.state.mute}
                resizeMode="cover"
                style={{ width: 300, height: 300 }}
                useNativeControls={true}
                isLooping={true}
            />
        </SafeAreaView>
    );
}


Here is the async function from another file, where the data is being pushed to firebase (another file, within in an onPress function).这是来自另一个文件的异步 function,其中数据被推送到 firebase(另一个文件,在 onPress 函数中)。

async () => {
              // if recording
              if (!recording) {
                setRecording(true)
                video = await cameraRef.recordAsync();
                //console.log('video', { video });

                //trigger firebase push array 
                videoRef.push({ 
                  //push video
                  video,
                 }).then((data)=>{
                     //success callback
                     console.log('data ' , data)
                 }).catch((error)=>{
                     //error callback
                     console.log('error ' , error)
                 })
                .then(console.log('new video: ', {video}), alert('video added'));

              } else {
                setRecording(false)
                cameraRef.stopRecording()
              }
            }

Here is a screenshot of the videoArray values in console.这是控制台中 videoArray 值的屏幕截图。 在此处输入图像描述

Not sure why it's so complicated, would really appreciate some help.不知道为什么这么复杂,非常感谢一些帮助。

UPDATE: Ive fixed the array, now it's a matter of getting the uir values.更新:我已经修复了数组,现在是获取 uir 值的问题。

Here is the updated array:这是更新后的数组:

after use effect Array [
  Object {
    "key": "-M40wgjXtE4utZUH37Fn",
    "video": Object {
      "uri": "file:///var/mobile/Containers/Data/Application/6D4BF03E-4E53-481A-AF86-55C6B702B6B0/Library/Caches/ExponentExperienceData/%2540ameer_devking%252Fspark-app/Camera/BADB80A8-27E9-4BDD-9779-81CC356B6F93.mov",
    },
  },
  Object {
    "key": "-M40wofZn8k81mE4DMb4",
    "video": Object {
      "uri": "file:///var/mobile/Containers/Data/Application/6D4BF03E-4E53-481A-AF86-55C6B702B6B0/Library/Caches/ExponentExperienceData/%2540ameer_devking%252Fspark-app/Camera/320B6F55-81F3-42A1-83C7-DE5F988F64B4.mov",
    },
  },
]

Here was the code I used to get the uir values, got error undefined is not an object (evaluating 'videoArray[1].map')这是我用来获取 uir 值的代码,得到错误undefined is not an object (evaluating 'videoArray[1].map')

Code used:使用的代码:

 const videoUris = videoArray[0].map(video => video.uri);
 console.log("URIS:",videoUris);

How to destructure and get the uri's:如何解构和获取uri:

   const videoUris = videoArray.map(item=> item.video.uri)
   console.log(videoUris);

Add to FlatList, example found in the comments of this post.添加到 FlatList,在这篇文章的评论中找到示例。 Click here点击这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM