【问题标题】:The "original" argument must be of type Function“原始”参数必须是函数类型
【发布时间】:2021-06-18 16:06:09
【问题描述】:

我是 react native 的新手,并试图将数据库与我的程序连接起来。我收到了这个错误,我不知道如何解决它...

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");

……还有这个……

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 | 

我正在使用 DataStax Astra 作为数据库,这就是我想要做的https://docs.datastax.com/en/astra/docs/connecting-to-your-database-with-the-datastax-nodejs-driver.html

这是我的代码...

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',
  },
});

【问题讨论】:

    标签: javascript node.js react-native datastax datastax-java-driver


    【解决方案1】:

    不要在组件内部使用 require 导入客户端,而是尝试在文件顶部进行常规导入:

    // 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';
    

    编辑:看起来 Cassandra 驱动程序是一个在后端与节点一起使用的工具,所以这个插件可能在 React-Native 中不起作用。所以你必须在 node 中设置一个后端,然后从那里获取 ReactNaive 中的结果。

    【讨论】:

    • 我只收到一个错误TypeError: The "original" argument must be of type Function,您能更好地解释一下如何在后端设置 cassandra 驱动程序以及如何在本机反应中获取结果吗?
    • 您可以尝试将import { Client } from 'cassandra-driver'; 移动到文件的最顶部吗?
    • 你有机会分享你服务器的代码 sn-p 吗?
    猜你喜欢
    • 2020-06-20
    • 2020-10-31
    • 2023-04-05
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2019-10-01
    • 1970-01-01
    相关资源
    最近更新 更多