Object updating instead of creating new db line when a live query subscription exists for same table

Hi I have a simple message pinboard. each new message gets sent in by clicking ‘send’ however instead of creating a new message in the db it just updates the previous (unless I refresh the app)

const send = msg => {
const PBMessage = moralis.Object.extend(PinBoardMessage)
const pbmessage = new PBMessage()
pbmessage.set(“message”, msg)
pbmessage
.save()
.then(res => {
console.log(“message saved to db”)
console.log(res)
message.value = “”
})
.catch(err => console.error(err))

maybe you can try without const there

Thanks I did but it didn’t make a difference. I even tried cleaning PMMessage and pbmessage after the send but still had the issue.

also this is a vue3 app. Maybe those variables are stored somehow although I have not seen this behaviour before.

can you look in your browser network tab to see what are the request parameters that are sent when trying to save that object?

Ok I’ll trying to explain without overcomplicating. I investigated the network tab. The first message sent after .save() the “Name” field of the networks tab is the class name (in my case the pinboard reference name). This adds a new row in the db for that class name. Now when I try to send the next(or any subsequent) message the ‘Name’ field is of the objectId of the first message sent and hence it just updates it. Now here I need to add that I have a subscription service running. I need this because I want any message sent to my pinboard obviously updated realtime. I have now removed this subscription and every thing works ok. BUT obviously I have to manually click a button to ‘loadMessages’ which isn’t desirable. I guess I can try LiveQuery.close() before sending every message but is that supposed to happen?

you say that that problem was because there was also a live query subscription at the same time?

Basically yes. If I remove subscription the db updates as expected

ok, I wouldn’t expect it to work like this, I’ll have to test it too, it looks like a problem if that is what it happens

ok thanks let me know. As a temporary fix I have now got it working the way I want but its scrappy. As I suggested - BEFORE sending the message I now use moralis.LiveQuery.close(). Then straight AFTER sending the message I have initSubscription function and then finally a function to manually reload messages (since I did not get the ‘create’ event for the message I sent) I still need to check that another logged in user will trigger the ‘create’ event

that live query, is on the same table or on a different table?

Yes on the same className/Table.

I tested with this code:

const send2 = msg => {
const PBMessage = Moralis.Object.extend("testa")
const pbmessage = new PBMessage()
pbmessage.set("x", msg)
pbmessage
.save()
.then(res => {
console.log("message saved to db")
console.log(res)
})
.catch(err => console.error(err))

}

and at the same time running a live query subscription with this code:

let query = new Moralis.Query('testa');
let subscription = await query.subscribe();
subscription.on('open', () => {
 console.log('subscription opened');
});

subscription.on('create', (object) => {
  console.log(object, 'object created');
});

subscription.on('update', (object) => {
  console.log(object, 'object updated');
});

subscription.on('close', () => {
  console.log('subscription closed');
});

and I didn’t have that problem of updating the same object when using send2("adfasdf") multiple times

Well rather embarrassingly this is working for me now also. I took out my ‘messy fix’ after reading your comments again just now and it just seemed to work. I really can’t think that I changed anything that would affect this. This was a bug that was affecting me for a couple of weeks and I thought I’ll leave it and come back to it so it. So…thanks for your help anyway.