React Native TypeError:_ExpoSecureStore.default.getValueWithKeyAsync不是函数

bnl4lu3b  于 2023-06-24  发布在  React
关注(0)|答案(2)|浏览(164)

我正在尝试在React Native中使用expo-secure-store。我得到下面的错误:

可能的未处理Promise拒绝(id:2):TypeError:_ExpoSecureStore.default.getValueWithKeyAsync不是函数。(在'_ExpoSecureStore.default.getValueWithKeyAsync(key,options)'中,'_ExpoSecureStore.default.getValueWithKeyAsync'未定义)

我试图寻找解决办法,但找不到。因此,我尝试使用docs中给出的示例代码进行检查,但错误仍然是类似的错误:可能的未处理承诺拒绝(id:1):错误:方法或属性SecureStore.setItemAsync在android上不可用,您确定已正确链接所有本机依赖项吗?
下面是我使用的代码:

import * as React from 'react';
import { Text, View, StyleSheet, TextInput, Button } from 'react-native';
import * as SecureStore from 'expo-secure-store';

async function save(key, value) {
  await SecureStore.setItemAsync(key, value);
}

async function getValueFor(key) {
  let result = await SecureStore.getItemAsync(key);
  if (result) {
    alert("🔐 Here's your value 🔐 \n" + result);
  } else {
    alert('No values stored under that key.');
  }
}

export default function App() {
  const [key, onChangeKey] = React.useState('Your key here');
  const [value, onChangeValue] = React.useState('Your value here');

  return (
    <View style={styles.container}>
      <Text style={styles.paragraph}>Save an item, and grab it later!</Text>
      {}

      <TextInput
        style={styles.textInput}
        clearTextOnFocus
        onChangeText={text => onChangeKey(text)}
        value={key}
      />
      <TextInput
        style={styles.textInput}
        clearTextOnFocus
        onChangeText={text => onChangeValue(text)}
        value={value}
      />
      {}
      <Button
        title="Save this key/value pair"
        onPress={() => {
          save(key, value);
          onChangeKey('Your key here');
          onChangeValue('Your value here');
        }}
      />

      <Text style={styles.paragraph}>🔐 Enter your key 🔐</Text>
      <TextInput
        style={styles.textInput}
        onSubmitEditing={event => {
          getValueFor(event.nativeEvent.text);
        }}
        placeholder="Enter the key for the value you want to get"
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: 10,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  paragraph: {
    marginTop: 34,
    margin: 24,
    fontSize: 18,
    fontWeight: 'bold',
    textAlign: 'center',
  },
  textInput: {
    height: 35,
    borderColor: 'gray',
    borderWidth: 0.5,
    padding: 4,
  },
});
olqngx59

olqngx591#

1.请确保使用通配符导入导入Secure Store:

import * as SecureStore from "expo-secure-store";

1.如果错误仍然存在,可能是因为您正在使用自定义开发人员客户端。您需要重新生成开发客户端,以便能够使用Secure Store库。

eoxn13cs

eoxn13cs2#

我也遇到过类似的问题,通过研究尝试了3种解决方案:
1.导入Secure Store,如下所示:

import * as SecureStore from "expo-secure-store"

1.安装并使用Secure Store后重新构建开发客户端。
1.请确保您使用正确的平台进行构建,例如Android设备或模拟器,或IOS设备或模拟器。Secure Store不支持Web平台。

相关问题