The "original" argument must be of type Function

2.7k views Asked by At

I am new with react native and trying to connect database with my programs. I am getting this error and I don't know what to try to fix it...

TypeError: The "original" argument must be of type Function

Profil
src/jsoni/Profil.js:4

  1 | import React from 'react';
  2 | import { StyleSheet, SafeAreaView,TouchableOpacity,Text} from 'react-native';
  3 | 
> 4 | export default function Profil({navigation}) {
  5 | 
  6 | 
  7 |   const { Client } = require("cassandra-driver");

...and this one...

Unhandled Rejection (TypeError): Client is not a constructor

registerRootComponent
src/launch/registerRootComponent.web.tsx:14

  11 |   const RootComponent: React.FC<P> = props => <App {...props} />;
  12 |   AppRegistry.registerComponent('main', () => RootComponent);
  13 |   const rootTag = document.getElementById('root') ?? document.getElementById('main');
> 14 |   AppRegistry.runApplication('main', { rootTag });
  15 | }
  16 | 

I am using DataStax Astra for database and here is what I am trying to do https://docs.datastax.com/en/astra/docs/connecting-to-your-database-with-the-datastax-nodejs-driver.html

This is my code...

import React from 'react';
import { StyleSheet, SafeAreaView,TouchableOpacity,Text} from 'react-native';

export default function Profil({navigation}) {

  const { Client } = require("cassandra-driver");
  async function run() {
    const client = new Client({
      cloud: {
        secureConnectBundle: "../../secure-connect-test.zip",
      },
      credentials: {
        username: "myusername",
        password: "mypassword",
      },
    });
    await client.connect();
  
    // Execute a query
    const rs = await client.execute("SELECT firstname FROM keyspace.table");
    console.log(rs.rows);
    await client.shutdown();
  }
  
  // Run the async function
  run();
  const press = () => {
    navigation.navigate('Home');
  }
  return (
  <TouchableOpacity onPress={press}>
      <SafeAreaView>
          <SafeAreaView style={styles.border}/>
          <Text>Text here</Text>
      </SafeAreaView>
  </TouchableOpacity>
  );
}
const styles = StyleSheet.create({
  border: {
    height: 50,
    width:100,
    backgroundColor:'#ff69ff',
  },
});
1

There are 1 answers

3
matthiasgiger On

Instead of importing the Client inside the component with require try a regular import at the top of the file:

// Remove this line
const { Client } = require("cassandra-driver");

// and import at the top of the file =>

import React from 'react';
import { ... } from 'react-native';
import { Client } from 'cassandra-driver';

Edit: Looks like the Cassandra Driver is a tool to be used in the backend with node, so probably this plugin will not work in React-Native. So you'll have to set up a backend in node and then fetch the results in ReactNaive from there.