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()

    // =  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)




        console.log("object created!")


    }else if(resultRoom || peerResult){


       setRoomId( = roomId

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

        await query.equalTo("roomId",

        const resultMessage =await query.find()

        console.log("Already created!")


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


        console.log("currentroom", currentRoom)

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





        console.log("Not working")





    async function writeMessage(e){


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

 = roomId


         console.log("Current room",

         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





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




            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 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.