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
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)
What is the messages attribute?
This line
messages: [...data],
Won't work as [] is array and data is an object
Then you need to go a different way on it.
messages: One-to-Many Relationship to another collection which named messages
{
message: string,
seen: boolean
}
First you'll need to fetch the current messages Then, you need to stringify the new Data and append it Something like so
const promise = this.chat3Service.updateRoom(this.roomId, {
messages: [...prev.message, JSON.stringify(data)], // error warning this line
});
Everytime pushing new messages, should i fetch all prev.messages ? Without fetching, can i do it ? any idea
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
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
{
message: string,
seen: boolean
}
doc ref: https://appwrite.io/docs/databases-relationships#create-nested
Got you
But messages: [...data], won't work in, javascript, what you can try to do is something like this
messages:[
...current.messages
{...data}
]
Recommended threads
- One to many 2 way, console UI not correc...
Hey, seems I'm facing the exactly same issue with this one: https://github.com/appwrite/appwrite/issues/6016 Since this Github issue stay open for so long, let ...
- Server Down
Appwrite services are down. When will they start working again?
- Scheduled works locking the entire Maria...
I have a scheduled function and apparently that or something is locking the entire MariaDB database and Appwrite is giving MariaDB errors. This error persists e...