首页 > 解决方案 > 带有 React Native 的 TypeORM

问题描述

我创建了一个新的虚拟应用程序react-native init test,然后按照说明添加 typeorm。在我的 App.js 中我包含import {getManager} from 'typeorm',然后运行react-native run-ios​​.

我在 Metro-bundler 中看到以下错误:

Error: Unable to resolve module path from /Users/amit/Code/test/node_modules/typeorm/platform/PlatformTools.js: Module path does not exist in the Haste module map

这是显示问题的示例存储库:在此处输入链接描述

不确定我是否错过了设置中的某些内容!非常欢迎任何帮助!

标签: react-nativesqlitetypeorm

解决方案


不幸的是,从“typeorm”模块导入不起作用,因为 react-native 项目不使用节点平台。从“typeorm/browser”导入将起作用。这是一个示例项目。:https ://github.com/typeorm/react-native-example

确保您创建的连接对象不使用对项目文件系统的任何引用。避免使用类似的东西:

import { CountSession } from '../biopro-mobile-database/entities/count_session';

   const connection = await createConnection({
            name: 'liteDb_3',
            type: 'react-native',
            database: 'biopro_mobile.sqlite',
            location: 'default',
            synchronize: false,
            logging: true,
            entities: ["../biopro-mobile-database/entities/**/*.ts"],
          })

避免使用实体: ["../biopro-mobile-database/entities/ /*.ts"],** 而是使用类似的东西:

import { EquipmentCounted } from '../biopro-mobile-database/entities/equipment_counted';
import { CountSession } from '../biopro-mobile-database/entities/count_session';

   const connection = await createConnection({
            name: 'liteDb_3',
            type: 'react-native',
            database: 'biopro_mobile.sqlite',
            location: 'default',
            synchronize: false,
            logging: true,
            entities: [
              CountSession,
              EquipmentCounted,
            ],
          })

推荐阅读