Moralis parent query returns "Cannot create a pointer to an unsaved ParseObject"

Hi! Iā€™m creating a dapp chat and trying to set a Room parent for the message. The Room has 2 peers, one is the main user that is initiating it and the other is the peer the message is sent to.The problem is when I try to add the peer to the room created by the main user as it created another one.Also the message between the two peers, how should it work because the null error pointer comes from messaging from the peer side as it says the currentRoom value is null. How can I do it so that both users can send message to one another in the same chat room? This is the code I have so far:

async function openChatRoom(event){

   // const params = {"userMain": event, "peer": user.get("username")}

   const currentUser = user.get("username")

   const queryRoom = new Moralis.Query("ChatRooms")

   const peerRoom = new Moralis.Query("ChatRooms")

    await peerRoom.equalTo("peer", currentUser)

    const peerResult = await peerRoom.find()

    //currentRoom.id =  roomId

   const subscription = await queryRoom.subscribe()

    subscription.on('open', async()=>{

        await queryRoom.equalTo("userMain",currentUser)

        const resultRoom = await queryRoom.first()

     

        console.log("Peer result",peerResult)

       // console.log(peerResult)

        if(!resultRoom && !peerResult){

        await currentRoom.set("userMain", currentUser)

        await currentRoom.set("peer",event)

        await currentRoom.save()

        setReceiver(event)

        setRoomId(currentRoom.id)

        console.log("object created!")

        alert("open!")

    }else if(resultRoom || peerResult){

       console.log(peerResult)

       setRoomId(peerResult.id)

       currentRoom.id = roomId

        const query = new Moralis.Query("Messages")

        await query.equalTo("roomId", peerResult.id)

        const resultMessage =await query.find()

        console.log("Already created!")

        setReceiver(event)

       // console.log("PEer",peerResult)

        console.log(peerResult)

        console.log("currentroom", currentRoom)

       // console.log(peerResult[0].attributes.peer)

        console.log(roomId)

        setMessages(resultMessage)

        console.log(messages)

    }else{

        console.log("Not working")

    }

    })

}

   

    async function writeMessage(e){

      console.log(roomId)

        if(e!='undefined' && e.length >0)

        {

         currentRoom.id = roomId

         console.log(roomId)

         console.log("Current room",currentRoom.id)

         const query = new Moralis.Query("ChatRooms")

        await currentMessage.set("message",e)

        await currentMessage.set("sender",userMain)

        await currentMessage.set("roomId",roomId)

        await currentMessage.set("read",false)

        await currentMessage.set("parent",currentRoom)//here i get the error on the peer side

        console.log(currentRoom)

        await currentMessage.save()

        console.log(message)

        console.log(roomId)

        const newMessage={ message:e, sender:userMain, createdAt:JSON.stringify(currentMessage.createdAt), read:false, roomId:roomId}

        setMessage(newMessage)

        console.log(newMessage.read)

        setMessages(messages=>[...messages,{

            attributes: newMessage

        }])

    }

    }

maybe it didnā€™t find any room on that line with const resultRoom = await queryRoom.first()

yes, i figured, but it doesnā€™t make any sense as it should return the value .Also I tried to access the attributes to check if the username exists as a peer and I had problems with it as it said that ā€œattributesā€ are undefined if the record was not found, but how can I check it so that it will work even if it is undefined?I have tried to check if the attributes property was undefined, null, 0 all the same result. Iā€™ve managed to to a cloud function to get the peer user like that and sometimes it worked, sometimes it didnā€™t although it should have fetched the record. I had to wait for a little bit after the cloud function was deployed before I could see results.

you may need to use a cloud function with master key in order to access info about other user

Did that already and what i described above happened. This is the cloud function code I used.

Moralis.Cloud.define("checkPeer", async(request)=>{

  const query = new Moralis.Query("ChatRooms")

    await query.equalTo("objectId", request.params.objectId)

    const peerKey = await query.find({useMasterKey:true})

    let value

    for(let i=0;i< peerKey.length;i++){

      value = await peerKey[i].get("peer")

    }

    return value

})

you could use some logging with logger.info(JSON.stringify(peerKey)) for example

Iā€™ve done that already and it has moments when it works and moments when it doesnā€™t saying that the value is undefined.

can you also log that request.params.objectId?

yes, now it returns the correct value. Iā€™ve spent hours only on that problem and apparently now itā€™s fixed for itself.What can you tell me about the attributes problem?Is there something i can do with the checking so that I wonā€™t have to deal every time with the ā€œundefinedā€ error?

what is the problem about the attributes?

Letā€™s say I get a record and I want to access a value inside attributes. Normally it will go: retrievedRecord.attributes.value. But if that value I want to retrieve does not exist, how can I check if it is undefined meaning: if(retrievedRecord.attributes.value === ā€˜undefinedā€™) or null or 0.

ok, I donā€™t know exactly how to do it, it should be a way to do it in javascript
maybe using exceptions, or .get(ā€˜attributesā€™), or to check first I retrievedRecord is undefined, then attributes, then value

ok, will try! Thanks for the help!

Found lots of info in this article thanks for sharing keep it up.