0

我正在删除一个领域数据库对象,但我想在删除后访问它的值,以便我可以在其他处理中使用它。这就是我的代码的样子:

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

我想使用/返回 productName 来做其他事情,但是当我尝试访问它时,我发现它undefinednull

我认为这是因为 theObject 已被删除,因此无法访问 productId,但我想知道为什么会这样,因为我在删除之前分配它时已经捕获了该 id。IEproductName = theObject[0].name

现在,我的问题是,我怎样才能仍然获取/访问/返回该产品名称?

4

1 回答 1

0

这应该是不可能的。注释掉删除和控制台日志 productName 以确保事情的行为与您认为的一样。

此外,您似乎缺少 productName 的 var/let/const

const productName = theObject[0].name

编辑:

由于这些是异步操作,因此可能real.objects('Prodcuts')会输掉realm.delete(). 您可以通过在尝试删除之前等待您的访问请求来解决此问题。

于 2021-12-19T14:31:29.397 回答