TransWikia.com

Vue js/Javascript Objects.assign() not working

Stack Overflow Asked by madsongr on November 7, 2021

I need to concatenate two objects in Vue/Javascript and order the resulting one by date field but I’m getting empty results using Object.assign().

I’ve tried other methods searching on StackOverflow but nothing has worked as expected. Mostly I get empty results

methods: {
        getAllMessages: function () {
            // console.log(this.myID);
            let distinct = '';

            firebase.firestore().collection('private-messages').where('memberID', '==', `${this.myID}`)
                .orderBy('date').onSnapshot(res => {

                    let members = [];
                    res.forEach(doc => {
                        members.push(doc.data().uid);
                    });
                    distinct = Array.from(new Set(members));
                    // console.log(distinct);

                    this.getUserToTalk(distinct);
                });
        },
        getMyMessages: function (memberID) {
            let myMessages = [];
            firebase.firestore().collection('private-messages').where('uid', '==', `${this.myID}`).where('memberID', '==', `${memberID}`)
                .orderBy('date').onSnapshot(res => {
                    res.forEach(doc => {
                        myMessages.push(doc.data());
                    });
                });
            // this.myMessages = myMessages;
            return myMessages;
        },
        getMessages: function (memberID) {

            let messages = [];
            firebase.firestore().collection('private-messages').where('uid', '==', `${memberID}`).where('memberID', '==', `${this.myID}`)
                .orderBy('date').onSnapshot(res => {
                    res.forEach(doc => {
                        messages.push(doc.data());
                    });
                });

            // this.memberMessages = messages;
            return messages;

        },
        getUserToTalk: function (memberID) {

            axios.post('http://localhost/backend/getMemberToTalk.php', {
                "token": token,
                "whoToTalkTo": memberID,
            }).then(response => {
                if (response.data != "Error getting user data and tour") {

                    let joinedData = []
                    // console.log(response.data);

                    response.data.forEach(res => {
                        
                        let id = res.memberID;

                        let messages = this.getMessages(id);
                        // console.log(messages);

                        let myMessages = this.getMyMessages(id);
                        // console.log(myMessages);

                        // if I use below assignment I can get the messages, but I need to concat them.
                        // so, if I use let conversation = Object.assign({}, messages, myMessages); 
                        // or let conversation =  { ...messages, ...myMessages }
                        // the result is always empty.

                        let talk1 = Object.assign(messages);
                        let talk2 = Object.assign(myMessages);
                        console.log(talk, talkmy);
                        
                        let data = {
                            memberID: res.memberID,
                            memberProfileImg: res.memberProfileImg,
                            memberName: res.memberName,
                            memberLastname: res.memberLastname,
                            memberCity: res.memberCity,
                            memberState: res.memberState,
                            memberMessages: messages,
                            myMessages: myMessages,
                            // messages: conversation
                        }

                        joinedData.push(data);
                    });
                    // console.log(joinedData);

                    this.memberData = joinedData;
                    // console.log(this.memberData);
                } else {
                    console.log(response.data);
                }
            }).catch(error => {
                console.log(error);
            });
        },
}

enter image description here

These are the full objects and in the example below I need to add the object from messages.js:140 as a "third index" in messages.js:137 resulting in

0:{...}

1:{...}

2:{...}

and then

3:{...}

enter image description here

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP