我已经使用RTM并研究了stackoverflow,但由于有限的文档而仍然失败。我有一个react native
realm
模式设置,添加了记录。它返回的记录数正确,但是我无法从中获取任何实际记录以进行迭代。
我只是想获取纬度/经度,然后放在react-native-maps
上。我想念什么?我的地图工作正常,只是无法从数据库中获取任何坐标返回的记录。
尝试遍历返回的记录/对象似乎是一个非常普遍的问题
Struct.js
import Utils from '../../helpers/utils'; //GUID gen only
class Struct {
constructor(name, lat, long, country) {
this.id = Utils.guid();
this.name = name;
this.lat = lat;
this.long = long;
this.country = country;
}
}
module.exports = Struct;
dbService.js
import Realm from 'realm';
import Struct from '../schema/Struct';
let repository = new Realm({
schema: [{
name: 'Struct',
primaryKey: 'id',
properties: {
id: {type: 'string', indexed: true},
name: 'string',
lat: 'string',
long : 'string',
country : 'string',
}
}],
schemaVersion: 1 //<--- needed to add this for any schema change, otherwise migration error occurs.
});
let dbService = {
getAll: function() {
//return repository.objects('Struct');
return repository.objects('Struct').filtered('country="UK"');
},
countAll: function() {
return repository.objects('Struct').length;
},
save: function(Struct) {
//if one already exists in country, dont add it.
if (repository.objects('Struct').filtered("country = '" + Struct.country + "'").length) return; //if record exists, exit
repository.write(() => {
//Struct.updatedAt = new Date();
repository.create('Struct', Struct);
})
},
};
dbService.save(new Struct('Object 1','xx.xxxxx','xxx.xxxxx','UK'));
dbService.save(new Struct('Object 2','xx.xxxxx','xxx.xxxxx','FR'));
dbService.save(new Struct('Object 3','xx.xxxxx','xxx.xxxxx','DE'));
dbService.save(new Struct('Object 4','xx.xxxxx','xxx.xxxxx','NL'));
dbService.save(new Struct('Object 5','xx.xxxxx','xxx.xxxxx','ES'));
module.exports = dbService ;
主应用
var db = require('./db/db/dbService');
let recCount = db.countAll();
console.log(recCount); //<---- This works correctly
let recs = db.getAll();
for (let rec of recs) {
console.log(rec); //<----- Never runs
}
从console.log()
返回的db.getAll()
是一个对象,但不是预期的格式,并且似乎是Realm Helper,而不是预期的已过滤对象的列表:
Proxy {Symbol(realm): 162, Symbol(id): 177, …}
[[Handler]]: Object
[[Target]]: Results
[[IsRevoked]]: false
非常感谢
编辑
实际上..
我说谎countAll()
函数返回5条记录,而将dbService.save()
行注释为仅1条时,它仍返回5条记录。