我使用Realm(JS)版本11.9.0和Electron 24.0.0。我尝试在Electron应用程序中设置本地数据库,但由于未知原因出错。当我用指定的配置打开Realm数据库时,我看到返回了一个空对象。
顺便说一句,创建数据库是因为我看到它出现在我的目录中,但只要我添加项并希望检索项,在这两种情况下我都会得到一个空对象。我也没有看到任何错误消息在我的电子日志环境或控制台。
所以很难准确指出哪里出了问题。我是不是漏掉了什么?
下面是示例代码:
main.ts
import Realm from "realm";
const TaskSchema = {
name: "Task",
properties: {
_id: "int",
name: "string",
priority: "int?",
progressMinutes: "int?",
},
primaryKey: "_id",
};
const PersonSchema = {
name: "Person",
properties: {
name: "string",
age: "int?",
},
};
const DogSchema = {
name: "Dog",
properties: {
name: "string",
owner: "Person?",
age: "int?",
},
};
const CatSchema = {
name: "Cat",
properties: {
name: "string",
},
};
let realm:Realm;
ipcMain.handle('createConnection', async (event, message) => {
console.log('message',message)
const encryptionKey = new Int8Array(64);
try {
realm = await Realm.open(
{
schema: [TaskSchema,PersonSchema,DogSchema,CatSchema],
path: "test.realm",
encryptionKey,
}
);
}catch(err){
console.log("check issue:",err)
}
return {
status: "ok",
test: realm // returns Realm {}
}
})
ipcMain.handle('createCat', async (event, message) => {
try {
let cat: Realm.Object;
realm.write(() => {
cat = realm.create("Cat", { name: "Max" });
});
}catch(err){
console.log("Check issue:",err)
}
console.log('message',message)
return {
status: "ok"
}
})
ipcMain.handle('getCat', async (event, message) => {
let cats = realm.objects("Cat");
console.log("cats: ",cats) // returens {}
return {
status: "ok",
}
})
有没有人在Realm-Nodejs和Electron中有更多的经验?
先谢谢你了。
1条答案
按热度按时间hts6caw31#
Realm对象是原生的,不能被JavaScript显示,但是你仍然可以访问它们的属性。