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
- Issue with downloading large files (40GB...
Hi everyone! I am using the latest Appwrite 1.8.0 version on my self-hosted server. I successfully uploaded a large ZIP archive (~40GB) using the chunked uploa...
- Cant get realtime working
Hey I nned some help with realtime a gain. I was using client.subscribe(...), and i found out that its depricated then i believe realtime.subscribe(...) is the ...
- Firebase app import
I'm **very** new to appwrite and I just set up appwrite with docker and I'm trying to import a Firebase app I have set up but it's erroring and I don't really k...