Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

TypeORM query returns an empty array

$
0
0

I'm trying to retrieve data from an existing database. I recently switched to bare workflow from Expo managed, trying to test react-native-sqlite-storage with TypeORM. However, TypeORM raw query keeps returning an empty array and I have no idea why.

Here's my code and it is based on this example https://dev.to/jgabriel1/expo-sqlite-typeorm-4mn8

DictionaryScreen.js

function DictionaryScreen(props) {    const { ejRepository } = useDatabaseConnection();    const handleinputWord = async () => {        console.log(props.inputWord);  //=>this console.logs the text from the input below         try {            const results = await ejRepository.getWordandMean(props.inputWord);            console.log(results); //=> Here I expect an array of the query result but it only returns []        } catch (error) {            console.log(error)        }    }    return(<View style={styles.searchContainer}><MaterialCommunityIcons name="text-search" color="white" size={30}/><TextInput                        style={styles.input}                        onChangeText={props.updateTextInput}                        onSubmitEditing={handleinputWord}                        value={props.inputWord}                        placeholder='Look up the word (Type exactly)'                        keyboardType="default"                    /> <StatusBar style='light' /></View>           )}const mapStateToProps = state => {    return {         inputWord: state.inputWord    }};const mapDispatchToProps = {updateTextInput};export default connect(mapStateToProps, mapDispatchToProps) (DictionaryScreen);

repository.ts

export class EJRepository {    private ormRepository: Repository<EJModel>;    constructor(connection: Connection) {      this.ormRepository = connection.getRepository(EJModel);    }    public async getWordandMean(props: any): Promise<EJModel> {      console.log(props);  //=> this returns the same text from DictionaryScreen      const results = await this.ormRepository.query(        `SELECT * FROM ejmodel WHERE word LIKE '%${props}%';`, [props]);      return results;    }}

connection.tsx

interface DatabaseConnectionContextData {    ejRepository: EJRepository;}const DatabaseConnectionContext = createContext<DatabaseConnectionContextData>(    {} as DatabaseConnectionContextData,  );export const DatabaseConnectionProvider: React.FC = ({ children }) => {   const [connection, setConnection] = useState<Connection | null>(null);   const connect = useCallback(async () => { const createdConnection = await createConnection({  type: 'react-native',  name: "ejdict.v1.0",  database: '***.db',  entities: [EJModel],           location: 'default',   migrations: [ejdict1621603544180],  migrationsRun: true,  synchronize: false,           logging: true,  extra: {createFromLocation: '/Users/***/***/ios/***/www/***.db' } }); setConnection(createdConnection); }, []);    useEffect(() => {      if (!connection) {        connect();      }    }, [connect, connection]);    if (!connection) {      return <ActivityIndicator />;    }return ( <DatabaseConnectionContext.Provider      value={{        ejRepository: new EJRepository(connection),      }}>      {children}</DatabaseConnectionContext.Provider>  );};export function useDatabaseConnection() {  const context = useContext(DatabaseConnectionContext);  return context;}

EJModel.ts

import {Entity, PrimaryColumn, Column} from "typeorm";@Entity({name: "ejmodel"})export class EJModel {    @PrimaryColumn({type: 'integer'})    item_id: number;    @Column({type: 'text'})    word: string;    @Column({type: 'text'})    mean: string;    @Column({type: 'integer'})    level: number;}

PS: Is it an unusual attempt to load an existing database on a React Native project? Working examples are almost no existent. I once made it work on Expo, but I realised I needed migration functionality and tried to do the same thing with TypeORM wasting lots of time...


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>