我正在尝试将数据库文件从资产导入博览会。但它只是不起作用并返回给我以下警告:
类型错误:未定义不是一个对象(评估'_expo.default.FileSystem')]
我尝试了很多次,如果我创建一个新数据库它可以工作,但是如果我尝试从资产加载现有数据库它将无法工作
class Items extends React.Component {
state = {
items: null
};
componentDidMount = async () => {
await Expo.FileSystem.downloadAsync(
Expo.Asset.fromModule(require("./assets/exu-word.db")).uri,
`${Expo.FileSystem.documentDirectory}SQLite/exu-word.db`
);
let db1 = SQLite.openDatabase("exu-word.db");
};
render() {
const { items } = this.state;
if (items === null || items.length === 0) {
return null;
}
return (
<View style={styles.sectionContainer}>
{items.map(({ id, words }) => (
<TouchableOpacity
key={id}
onPress={() => this.props.onPressItem && this.props.onPressItem(id)}
style={{
backgroundColor: "#fff",
borderColor: "#000",
borderWidth: 1,
padding: 8
}}
>
<Text style={{ color: "#000" }}>{words}</Text>
</TouchableOpacity>
))}
</View>
);
}
update() {
db.transaction(tx => {
tx.executeSql(
`select * from WORDS;`,
[],
(_, { rows: { _array } }) => this.setState({ items: _array })
);
});
}
}
Run Code Online (Sandbox Code Playgroud)
我与"sdkVersion": "35.0.0". 似乎 Expo 改变了它的 API。现在您需要安装一个单独的依赖项:
npm i --save expo-file-system
Run Code Online (Sandbox Code Playgroud)
然后FileSystem为您的组件独立导入对象:
import * as FileSystem from 'expo-file-system';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2792 次 |
| 最近记录: |