如何在领域数据库中保存数据 javascript
how to save data in realm database javascript
我不熟悉 react-native 并在我的应用程序中使用领域数据库。我想通过 json 预填充我的数据。我尝试使用 componentDidMount() 函数并使用 for 循环插入数据。任何指导都会有所帮助。
以下是我的代码:
// 模式文件:
import booksdata from './books.json';
const Realm = require('realm');
const BooksSchema = {
name: 'Books',
primaryKey: 'id',
properties: {
id: 'int', // primary key
name: 'string',
author: 'string',
publisher: 'string',
},
};
const databaseSchema = {
path: 'books.realm',
schema: [BooksSchema],
schemaVersion: 0, // optional
};
export const mountData = () => new Promise((resolve, reject) => {
Realm.open(databaseSchema)
.then((realm) => {
// Create Realm objects and write to local storage
realm.write(() => {
booksdata.forEach(obj => realm.create(databaseSchema, {
id: Math.floor(Date.now() / 1000),
name: obj.name,
author: obj.author,
publisher: obj.publisher,
}));
});
});
});
// 在我的索引文件中,调用了 componentDidMount :
componentDidMount() {
mountData().then().catch((error) => {
alert(`Insert error ${error}`);
});
}
// 我收到以下警告
Possible Unhandled Promise Rejection (id: 0):
Error: objectType must be of type 'string', got ([object Object])
Error: objectType must be of type 'string', got ([object Object])
at sendRequest (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:62352:45)
at sendRequest (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:62385:24)
at Object.callMethod (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:62128:22)
at Realm.<anonymous> (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:61983:28)
at blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:61408:15
at tryCallOne (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:16056:14)
at blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:16157:17
at blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:2884:21
at _callTimer (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:2773:9)
at _callImmediatesPass (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:2809:9)
您可以将 JSON 数据解析为 JavaScript 个对象,但对象必须与您的架构匹配。
let objs = JSON.parse(data);
realm.write(() => {
objs.forEach((obj) => realm.create('data', obj));
}
其中 'data'
是架构的名称。
谢谢大家!
我犯了一个非常愚蠢的错误。在我的架构文件中,当我在领域中插入一条记录时,我没有传递我的架构名称。我应该传递架构名称而不是 databaseSchema,即 BooksSchema.name (Books)
export const mountData = () => new Promise((resolve, reject) => {
Realm.open(databaseSchema)
.then((realm) => {
// Create Realm objects and write to local storage
realm.write(() => {
booksdata.forEach(obj => realm.create(BooksSchema.name, {
id: Math.floor(Date.now() / 1000),
name: obj.name,
author: obj.author,
publisher: obj.publisher,
}));
});
});
});
我不熟悉 react-native 并在我的应用程序中使用领域数据库。我想通过 json 预填充我的数据。我尝试使用 componentDidMount() 函数并使用 for 循环插入数据。任何指导都会有所帮助。
以下是我的代码:
// 模式文件:
import booksdata from './books.json';
const Realm = require('realm');
const BooksSchema = {
name: 'Books',
primaryKey: 'id',
properties: {
id: 'int', // primary key
name: 'string',
author: 'string',
publisher: 'string',
},
};
const databaseSchema = {
path: 'books.realm',
schema: [BooksSchema],
schemaVersion: 0, // optional
};
export const mountData = () => new Promise((resolve, reject) => {
Realm.open(databaseSchema)
.then((realm) => {
// Create Realm objects and write to local storage
realm.write(() => {
booksdata.forEach(obj => realm.create(databaseSchema, {
id: Math.floor(Date.now() / 1000),
name: obj.name,
author: obj.author,
publisher: obj.publisher,
}));
});
});
});
// 在我的索引文件中,调用了 componentDidMount :
componentDidMount() {
mountData().then().catch((error) => {
alert(`Insert error ${error}`);
});
}
// 我收到以下警告
Possible Unhandled Promise Rejection (id: 0):
Error: objectType must be of type 'string', got ([object Object])
Error: objectType must be of type 'string', got ([object Object])
at sendRequest (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:62352:45)
at sendRequest (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:62385:24)
at Object.callMethod (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:62128:22)
at Realm.<anonymous> (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:61983:28)
at blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:61408:15
at tryCallOne (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:16056:14)
at blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:16157:17
at blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:2884:21
at _callTimer (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:2773:9)
at _callImmediatesPass (blob:http://localhost:8081/d198fbe8-8b32-4037-93a7-df5a46af4f15:2809:9)
您可以将 JSON 数据解析为 JavaScript 个对象,但对象必须与您的架构匹配。
let objs = JSON.parse(data);
realm.write(() => {
objs.forEach((obj) => realm.create('data', obj));
}
其中 'data'
是架构的名称。
谢谢大家! 我犯了一个非常愚蠢的错误。在我的架构文件中,当我在领域中插入一条记录时,我没有传递我的架构名称。我应该传递架构名称而不是 databaseSchema,即 BooksSchema.name (Books)
export const mountData = () => new Promise((resolve, reject) => {
Realm.open(databaseSchema)
.then((realm) => {
// Create Realm objects and write to local storage
realm.write(() => {
booksdata.forEach(obj => realm.create(BooksSchema.name, {
id: Math.floor(Date.now() / 1000),
name: obj.name,
author: obj.author,
publisher: obj.publisher,
}));
});
});
});