简体   繁体   English

尽管条件为真,但 Array.filter() 返回空数组

[英]Array.filter() returning empty array although condition is true

I am trying to use filter to create a new array coming from firebase-firestore我正在尝试使用过滤器创建一个来自 firebase-firestore 的新数组

this is the filter function- when console loging the result of myTweet.userId === firebase.auth().currentUser.uid it returns true.这是过滤器功能 - 当控制台记录myTweet.userId === firebase.auth().currentUser.uid的结果时,它返回 true。

  isEqualToMyTweets = (myTweet) => {
        console.log(myTweet.userId)
        console.log(firebase.auth().currentUser.uid)
        console.log(myTweet.userId === firebase.auth().currentUser.uid)
        return myTweet.userID == firebase.auth().currentUser.uid;
    }

yet when calling the function it returns an empty array:然而,当调用 function 时,它返回一个空数组:

    onListenForTweets() {
        this.unsubscribe = firebase.firestore()
            .collection('tweets')
            .orderBy('date', 'desc')
            .limit(this.state.limit)
            .onSnapshot((snap) => {
                const tweets = snap.docs.map(docs =>
                    ({ ...docs.data(), id: docs.id })
                   
                );
                this.setState({ tweets })

                let myTweets = tweets.filter(this.isEqualToMyTweets);               
            });
    }

the reason I'm doing this is that i want to introduce a condition and accordingly present the tweets of a certain user.我这样做的原因是我想引入一个条件并相应地呈现某个用户的推文。

this is the components code:这是组件代码:


    constructor(props) {
        super(props);
        this.state = {
            tweets: [],
            loading: false,
            profile: "",
            interval: null,
            limit: 10,
        };
        this.myTweets = []
    }

    componentDidMount() {
        this.onListenForTweets()
    }

    isEqualToMyTweets = (myTweet) => {
        console.log(myTweet.userId)
        console.log(firebase.auth().currentUser.uid)
        console.log(myTweet.userId === firebase.auth().currentUser.uid)
        return myTweet.userID == firebase.auth().currentUser.uid;
    }

    onListenForTweets() {
        this.unsubscribe = firebase.firestore()
            .collection('tweets')
            .orderBy('date', 'desc')
            .limit(this.state.limit)
            .onSnapshot((snap) => {
                const tweets = snap.docs.map(docs =>
                    ({ ...docs.data(), id: docs.id })
                   
                );
                this.setState({ tweets })
                let myTweets = tweets.filter(this.isEqualToMyTweets);
                console.log(myTweets)
            });
    }

    async handleOnNewTweet(newTweet) {
        await firebase.firestore()
            .collection('tweets')
            .add(newTweet)
    }

    componentWillUnmount() {
        this.unsubscribe();
    }

this is an example of the console.log这是 console.log 的一个例子

TweetRoom.jsx:38 c1btNa39vZe3ptPfuYUn2chUsAn1
TweetRoom.jsx:39 c1btNa39vZe3ptPfuYUn2chUsAn1
TweetRoom.jsx:40 true
TweetRoom.jsx:38 c1btNa39vZe3ptPfuYUn2chUsAn1
TweetRoom.jsx:39 c1btNa39vZe3ptPfuYUn2chUsAn1
TweetRoom.jsx:40 true
TweetRoom.jsx:38 c1btNa39vZe3ptPfuYUn2chUsAn1
TweetRoom.jsx:39 c1btNa39vZe3ptPfuYUn2chUsAn1
TweetRoom.jsx:40 true
TweetRoom.jsx:38 c1btNa39vZe3ptPfuYUn2chUsAn1
TweetRoom.jsx:39 c1btNa39vZe3ptPfuYUn2chUsAn1
TweetRoom.jsx:40 true
TweetRoom.jsx:38 c1btNa39vZe3ptPfuYUn2chUsAn1
TweetRoom.jsx:39 c1btNa39vZe3ptPfuYUn2chUsAn1
TweetRoom.jsx:40 true
TweetRoom.jsx:38 c1btNa39vZe3ptPfuYUn2chUsAn1
TweetRoom.jsx:39 c1btNa39vZe3ptPfuYUn2chUsAn1
TweetRoom.jsx:40 true
TweetRoom.jsx:56 []length: 0__proto__: Array(0)

Posting this as a community wiki as this solution was pointed out in the comments to this thread by @PeterSeliger.将此作为社区 wiki 发布,因为@PeterSeliger 在对该线程的评论中指出了此解决方案。

The issue is being caused by the final call of your isEqualToMyTweets function where you are calling myTweet.userID instead myTweet.userId without the final capital letter, thus the comparison always fails and you get an entirely empty array as a result.该问题是由您的isEqualToMyTweets function 的最终调用引起的,您在其中调用myTweet.userID而不是myTweet.userId而没有最后的大写字母,因此比较总是失败,结果是一个完全空的数组。

So changing it to myTweet.userId will solve the issue.因此将其更改为myTweet.userId将解决问题。

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

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