Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix react-native example project #890

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 55 additions & 21 deletions examples/react-native/App.js
Expand Up @@ -13,7 +13,28 @@ import React from 'react';
const { width, height } = Dimensions.get('window');

import { default as randomToken } from 'random-token';
import * as RxDB from '../../';

// We have to do a custom build here because the default validate plugin,
// is-my-json-valid, only works in the node environment.
// See https://github.com/pubkey/rxdb/blob/c322b95422e37e9ce20513fdfca784077ad79020/docs-src/custom-build.md
// for more plugins that you can add.
import RxDB from 'rxdb/plugins/core';
import RxDBReplicationModule from 'rxdb/plugins/replication';
RxDB.plugin(RxDBReplicationModule);
import RxDBAjvValidateModule from 'rxdb/plugins/ajv-validate';
RxDB.plugin(RxDBAjvValidateModule);

// required if the database has a password
import RxDBEncryptionModule from 'rxdb/plugins/encryption';
RxDB.plugin(RxDBEncryptionModule);

// not required but important for development
import RxDBSchemaCheckModule from 'rxdb/plugins/schema-check';
RxDB.plugin(RxDBSchemaCheckModule);
import RxDBErrorMessagesModule from 'rxdb/plugins/error-messages';
RxDB.plugin(RxDBErrorMessagesModule);


import schema from './src/Schema';

RxDB.plugin(require('pouchdb-adapter-asyncstorage').default);
Expand All @@ -36,27 +57,36 @@ export default class App extends React.Component {
this.subs = [];
}
async createDatabase() {
const db = await RxDB.create({
name: dbName,
adapter: 'asyncstorage',
password: 'myLongAndStupidPassword',
multiInstance: false,
});
const heroCollection = await db.collection({
name: 'heroes',
schema,
});
heroCollection.sync({
remote: syncURL + dbName + '/',
options: {
live: true,
retry: true,
},
});
return db;
try {
const db = await RxDB.create({
name: dbName,
adapter: 'asyncstorage',
password: 'myLongAndStupidPassword',
multiInstance: false,
});
const heroCollection = await db.collection({
name: 'heroes',
schema,
});
heroCollection.sync({
remote: syncURL + dbName + '/',
options: {
live: true,
retry: true,
},
});
return db;
} catch(error) {
console.log(error)
}
}
async componentDidMount() {
this.db = await this.createDatabase();

try {
this.db = await this.createDatabase();
} catch(err) {
console.log(err);
}

const sub = this.db.heroes
.find()
Expand All @@ -76,7 +106,11 @@ export default class App extends React.Component {
console.log('addHero: ' + name);
const color = this.getRandomColor();
console.log('color: ' + color);
await this.db.heroes.insert({ name, color });
try {
await this.db.heroes.insert({ name, color });
} catch(error) {
console.log(error);
}
this.setState({ name: '', color: '' });
}
getRandomColor() {
Expand Down