Mutations run in an weird order

So basically I want to create a room using a question and name a user inputs, which would be the input and name state variables. The room is created every time, which is what I want since I need the room id to add the user. The issue is that when the user submits the create a room form, the member is not created nor are they added to the room initially. However, when the user resubmits the form with the same data, a new room is created and the new member is created in the database. I want the member and room to be created on the first submit of the form, but can't seem to figure out why it isn't working. Any help would be appreciated. Here is the snippet of code:
e.preventDefault();
// create a room
room.mutateAsync({
question: input
});



if (room && room.data){

member.mutate({
name,
roomId: room.data.id,
isHost: true,


});
e.preventDefault();
// create a room
room.mutateAsync({
question: input
});



if (room && room.data){

member.mutate({
name,
roomId: room.data.id,
isHost: true,


});
And here is my schema I'm using:
model Room {
id String @id @default(cuid())
question String
answers Answer[]
members Member[]
}

model Member {
id String @id @default(cuid())
isHost Boolean
name String
roomId String
room Room @relation(fields: [roomId], references: [id])
answers Answer[]
}
model Room {
id String @id @default(cuid())
question String
answers Answer[]
members Member[]
}

model Member {
id String @id @default(cuid())
isHost Boolean
name String
roomId String
room Room @relation(fields: [roomId], references: [id])
answers Answer[]
}
3 Replies
amanuel
amanuel3y ago
mutateAsync returns a promise, so make sure you await it
packofm&ms
packofm&msOP3y ago
i tried that too wait yea i did that wait i think i might be dum ok i fixed it ya so i wasn't using the result from mutateasync and ye i also need to await it tanks
amanuel
amanuel3y ago
I'm glad it worked out!!
Want results from more Discord servers?
Add your server