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.