gpt4 book ai didi

reactjs - 如何在 React Native 中导入 sequelize 库?

转载 作者:行者123 更新时间:2023-12-03 22:22:12 24 4
gpt4 key购买 nike

我在 react-native 项目中将 https://github.com/sequelize/sequelize 与 sqlite 一起使用。但是安装后,我导入

import Sequelize from 'sequelize';

并运行项目我收到此错误。请帮忙!谢谢你。

enter image description here

最佳答案

我使用 react-native init 设置了一个快速演示应用程序来尝试自己。我遇到了同样的错误。

在您的浏览器中,打开您的包 URL 您会更清楚地看到错误。 http://packager.e3-nmz.tuanbaros.demo-sqlite.exp.direct/node_modules/react-native-scripts/build/bin/crna-entry.bundle?platform=android&dev=true&strict=false&minify=false&hot=false&assetPlugin=/home/nguyen.thanh.tuan/Downloads/learning/react-native/demo-sqlite/mode_module/expo/tools/hashAssetFiles

这是错误的关键部分......

TransformError: [...]/node_modules/sequelize/lib/sequelize.js: require() must have a single string literal argument

我不确定这意味着什么,但是打包程序在解析 sequelize 包本身时遇到了麻烦。我建议你打开 sequelize repo 的问题。

仅供引用,这是我编写的用于测试的代码...
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/

import React, { Component } from 'react';
import {
StyleSheet,
Text,
View
} from 'react-native';

import Sequelize from 'sequelize';

export default class App extends Component<{}> {

state = {
connection: 'not yet connected'
}

componentDidMount() {
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'sqlite',
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},

storage: ':memory:'
});

sequalize
.authenticate()
.then( () => {
this.setState({ connection: 'Success: SQLite Connection'});
})
.catch( err => {
this.setState({ connection: 'Error: ${err}'})
});
}

render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Let's try to sequelize, shall we?
</Text>
<Text style={styles.instructions}>
{this.state.connection}
</Text>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

关于reactjs - 如何在 React Native 中导入 sequelize 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47149321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com