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

Error AlreadyHasActiveConnectionError:Cannot create a new connection named 'default' #6

Open
ahmadtlz opened this issue Mar 14, 2020 · 1 comment

Comments

@ahmadtlz
Copy link

hi ,
the first time i reload the app (ctrl+m ==> reload )is work perfect but when i exit the app and again run it the warning popup
i wanna close the connection but i don't no how :(
this is my code in react-native App

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      savedPost: false, progress: 'Post is being saved', loadedPost: [],
      textDescription: '',
      title: '',
      category: '',
      birthDate: '1998-8-02',
      author: ''
    };
    this.runDemo();
  }


  connect() {
    return createConnection({
      type: 'react-native',
      database: 'test',
      location: 'default',
      logging: ['error', 'query', 'schema'],
      synchronize: true,
      entities: [
        Author,
        Category,
        Post
      ]
    });``


  async runDemo() {
    try {
      // SQLite.openDatabase({ name: 'my.db', location: 'Library' }, this.successCB, this.errorCB);
      await this.connect();


      const category1 = new Category();
      category1.name = "TypeScript";

      const category2 = new Category();
      category2.name = "Programming";

      const author = new Author();
      author.name = "Person";

      const post = new Post();
      post.title = "Control flow based type analysis";
      post.text = "TypeScript 2.0 implements a control flow-based type analysis for local  variables and parameters.";
      post.categories = [category1, category2];
      post.author = author;

      const postRepository = getRepository(Post);
      // await postRepository.save(post);

      console.log("Post has been saved");
      this.setState({
        progress: "Post has been saved"
      });

      const loadedPost = await postRepository.find({ id: Between('10', '12') });


      // createQueryBuilder('post')
      // .innerJoinAndSelect("post.author", "author")
      // .innerJoinAndSelect('post.categories', 'categories')
      // .where('post.id = :id', { id: post.id })
      // .getOne();

      if (loadedPost) {
        console.log("Post has been loaded: ", loadedPost);
        this.setState({
          loadedPost: loadedPost
        });
      }
    } catch (err) {
console.log(err)
  }

1

2

3

@arantessergio
Copy link

Hey @Dking01, I made a workaround, verifying if the connection exists and if it's connected before create a new one.


                const exists = manager.has('default')

                let conn: Connection

                if (exists) {
                    const defaultConn = manager.get('default')

                    if (defaultConn) {
                        conn = defaultConn

                        if (!conn.isConnected) {
                            await conn.connect()
                        }
                    }
                } else {
                    conn = manager.create({
                        name: 'default',
                        type: 'react-native',
                        database: 'myappdb',
                        location: 'default',
                        logging: ['error', 'query', 'schema'],
                        synchronize: true,
                        entities: [
                            Entity
                        ]
                    })

                    await conn.connect()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants