[英]Realm object list functions are undefined, even though object is defined and list is populated
Trying to push a new int into an int realm list. 尝试将新int推入int领域列表。 I can get the Realm object, I can print it and when I print the object it shows the list with it's content, I can use Realm Studio to edit, add numbers to the list, etc.. But when I try to call any methods form the list it says it is undefined. 我可以获取Realm对象,我可以打印它,当我打印对象时,它显示带有它内容的列表,我可以使用Realm Studio进行编辑,将数字添加到列表中等等。但是当我尝试调用任何方法时从列表中可以看出它是未定义的。
Have tried async, await, then, though it was a synchronization issue, but doesn't seem like it. 尝试了异步,等待,然后,虽然这是一个同步问题,但似乎不是这样。
The code below is similar to mine, but edited to hide the original names, etc, and does not have all the properties from the original, but it does not change the behave for the specific list I am trying to edit. 下面的代码与我的类似,但编辑后隐藏原始名称等,并且没有原始属性的所有属性,但它不会更改我尝试编辑的特定列表的行为。 Everything else works fine. 其他一切都很好。 I have an schema like 我有一个类似的架构
let mySchema={
name:'MySchema',
properties:{
my_schema_id: 'string',
numbers: 'int[]'
}
The function to create a new object is 创建新对象的功能是
Realm.open({schema: [mySchema]})
.then(realm => {
realm.write(() => {
realm.create('MySchema', {my_schema_id: `${my_schema_id}`, numbers: [parseInt(number, 10)]});
});
I try to add a number with: 我尝试添加一个数字:
Realm.open({schema: [mySchema]})
.then((realm) => {
let fetchedSchema = realm.objects('MySchema').filtered(`my_schema_id ="${my_schema_id}"`);
console.log(fetchedSchema);
realm.write(()=>{
fetchedSchema.numbers.push(parseInt(number, 10));
});
And it gives an error: 它给出了一个错误:
(node:73249) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined
I expected to push the item to the list, or to be able to call the list functions. 我希望将项目推送到列表,或者能够调用列表函数。 Instead, when I try to use the list it shows as undefined
even though I can see it and it's content when I print the object... 相反,当我尝试使用列表时,它显示为undefined
即使我可以看到它,当我打印对象时它是内容...
Found a solution. 找到了解决方案。 Querying let fetchedSchema = realm.objects('MySchema').filtered(
my_schema_id ="${my_schema_id}" );
查询let fetchedSchema = realm.objects('MySchema').filtered(
my_schema_id =“$ {my_schema_id}” );
actually returns an array of elements, so I just had to select the first with [0]
at the end 实际上返回一个元素数组,所以我只需要在结尾选择第一个[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.