Amani
Amani

Reputation: 247

Realm, Accessing the Object property after deleting that Object

I am deleting a Realm db Object, but I want to access its value after deleting so that I can use it in other processings. This is how my code looks like:

const theObject = realm.objects('Products').filtered("_id == $0", "61bba17fc7e82eaae53527de")

//the fetched theObject(response) looks like this: 
[{_id:'61bba17fc7e82eaae53527de', name: 'Sugar'}]

var productName = theObject[0].name

realm.write(() => {

 realm.delete(theObject)

})

return productName //I want to use this productName here to do some other stuffs

I want to use/return the productName for doing some other things, but when i try to access it, i find it undefined or null.

I think this is caused because the theObject has been deleted and hence the productId can't be acceced, but I wonder why it is that way since i already caught that id initially when I assigned it before deleting. ie productName = theObject[0].name

Now,My question is, How can I still get/access/return that productName?

Upvotes: 0

Views: 318

Answers (1)

Spankied
Spankied

Reputation: 1885

This shouldn't be possible. Comment out the delete, and console log productName to ensure things are behaving as you think they are.

Also it looks like your missing a var/let/const for productName

const productName = theObject[0].name

Edit:

Since these are async operations its possible that real.objects('Prodcuts') is losing the race to realm.delete(). You can solve this by awaiting your access request before attempting to deleting.

Upvotes: 0

Related Questions