Back

Append data to one-to-many relational attribute

  • 1
  • Databases
  • Web
xue
20 Sep, 2023, 12:18
TypeScript
  addMessage() {
    const data = {
      message: '!!! 2nd message !!!',
      sender: this.currentUserId,
    };
    const promise = this.chat3Service.getRoom(this.roomId).then(room => {
      room.messages.push(data);
      return this.chat3Service.updateRoom(this.roomId, room);
    });
    promise.then(
      function (response) {
        console.log(response); // Success
      },
      function (error) {
        console.log(error); // Failure
      }
    );
  }

Copilot suggesting me to get all data first then push and update the relation again.

Is there any better way to do that ? I was trying to do

TypeScript
  addMessage() {
    const data = {
      message: '!!! 2nd message !!!',
      sender: this.currentUserId,
    };
    const promise = this.chat3Service.updateRoom(this.roomId, {
      messages: [...data], // error warning this line
    });
    promise.then(
      function (response) {
        console.log(response); // Success
      },
      function (error) {
        console.log(error); // Failure
      }
    );
  }

but i am getting following error Type '{ message: string; sender: string; }' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)

TL;DR
The user is asking for help with appending data to a one-to-many relational attribute in JavaScript. They have tried using spread syntax and fetching data, but they are encountering errors. They have provided code snippets for reference. The solution is to fetch the current messages, stringify the new data, and append it to the existing messages array. The user is also asking if there is a better way to achieve this without fetching all the previous messages.
Binyamin
20 Sep, 2023, 13:12

What is the messages attribute?

Binyamin
20 Sep, 2023, 13:12

This line

TypeScript
 messages: [...data],
Binyamin
20 Sep, 2023, 13:14

Won't work as [] is array and data is an object

Binyamin
20 Sep, 2023, 13:18

Then you need to go a different way on it.

xue
20 Sep, 2023, 13:20

messages: One-to-Many Relationship to another collection which named messages

TypeScript
{ 
  message: string,
  seen: boolean
}
Binyamin
20 Sep, 2023, 13:20

First you'll need to fetch the current messages Then, you need to stringify the new Data and append it Something like so

TypeScript

  const promise = this.chat3Service.updateRoom(this.roomId, {
      messages: [...prev.message, JSON.stringify(data)], // error warning this line
    });
xue
20 Sep, 2023, 13:22

Everytime pushing new messages, should i fetch all prev.messages ? Without fetching, can i do it ? any idea

Binyamin
20 Sep, 2023, 13:24

As of now, there is no way to update just re-fetch 😦 Maybe changing the logic to message collection with the chat (or user) id for the relationship

xue
20 Sep, 2023, 13:29

actually, as you see, its pointing to an id from messages collection, but in docs, if i want to update relational documents, i understand to update parent documents with new obj which is

TypeScript
{ 
  message: string,
  seen: boolean
}

doc ref: https://appwrite.io/docs/databases-relationships#create-nested

Binyamin
20 Sep, 2023, 13:33

Got you But messages: [...data], won't work in, javascript, what you can try to do is something like this

TypeScript
messages:[
    ...current.messages
    {...data}
]
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more