微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

“原始”参数必须是函数类型

如何解决“原始”参数必须是函数类型

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

解决方法

不要使用 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 中获取结果。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。