标签: asyncstorage

React Native - 更新AsyncStorage项嵌套键值?

我有一个用户存储通过AsyncStorage这样:

// data has a few key/value pairs for the user object
AsyncStorage.setItem('user', JSON.stringify(data));
Run Code Online (Sandbox Code Playgroud)

一个在键/值对dataquestion_count与值10

当用户删除问题时,如何将question_count值减1,AsyncStorage以便值为9

这是我尝试过的:

AsyncStorage.getItem('user').then(data => {
    data.question_count--;
});
Run Code Online (Sandbox Code Playgroud)

但这并没有改变价值.知道怎么做到这一点?

react-native asyncstorage

2
推荐指数
1
解决办法
5393
查看次数

redux-persist/getStoredState:恢复密钥数据时出错:xxxxx

redux-persist一直对我来说使用较小的状态树,但尝试在较大的状态树上使用它我在重新启动应用程序时遇到这些错误:

redux-persist/getStoredState:恢复key的数据时出错:pos错误:无法从CursorWindow读取第0行col 0.确保之前正确初始化了Cursor

无法从CursorWindow读取第0行,第0列.在从中访问数据之前,请确保正确初始化Cursor.错误:无法从CursorWindow读取第0行,第0列.在从中访问数据之前,请确保正确初始化Cursor.

我在MainApplication.java中尝试过类似的东西 - onCreate方法:*long size = 50L*1024L*1024L; // 50 MB com.facebook.react.modules.storage.ReactDatabaseSupplier.getInstance(getApplicationContext()).setMaximumSize(size)*

但似乎行不通.

提前致谢

react-native redux asyncstorage

2
推荐指数
1
解决办法
829
查看次数

如何在Android上正确阅读AsyncStorage

似乎可以AsyncStorage从Android(本机)访问;但我仍在努力使这项工作。

在我的React Native应用程序中,我有类似以下内容:

商店

const config = {
  ...
}

export const reducers = {
  ...
  user: user
}


let rootReducer = combineReducers(reducers)
let reducer = persistReducer(config, rootReducer)
Run Code Online (Sandbox Code Playgroud)

用户减速器

export class Actions {
    ...
}

const initialState = {
      first_name: : null,
      last_name: : null,
      email: null,
      addresses: [
          { ... }
      ]
    }
  }
}

export const reducer = (state = initialState, action) => {
  switch (action.type) {
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

从商店获取用户

const user = store.getState().user
console.log(user.first_name) …
Run Code Online (Sandbox Code Playgroud)

android react-native redux asyncstorage redux-persist

2
推荐指数
1
解决办法
2130
查看次数

React Native-返回AsyncStorage中的所有JSON数据?

在我的React Native应用程序的AsyncStorage中,我有多个JSON对象,每个对象都有一个唯一的键ID,如下所示:

 '62834456':
 data: { "foo": "bar",
 "nicknames": [ "grizz", "example" ],
 ... and so on }
Run Code Online (Sandbox Code Playgroud)

它们已被推入字符串化的AsyncStorage中。我正在尝试通过其ID检索每个对象,并将ID及其JSON数据都推入组件的状态。到目前为止,我有:

// for every key in async storage, push to favorites in state
importData = (id) => {
  for (id in AsyncStorage) {
    return AsyncStorage.getItem(id)
      .then(req => JSON.parse(req))
      .then(json => console.log(json))
      .catch(error => console.log('error!'));
  }
}
Run Code Online (Sandbox Code Playgroud)

当在上述函数中使用console.logging'json'时,结果为null。如何正确访问AsyncStorage中的所有JSON对象?

最终编辑

使用您的代码示例并删除JSON.parse(因此只需console.logging req)将返回以下内容: 在此处输入图片说明

出现这种情况的原因是因为某种原因。forEach返回了数组中的第一个字符串,数组本身,然后是第二个字符串。

json asynchronous reactjs react-native asyncstorage

2
推荐指数
2
解决办法
3823
查看次数

如何使 React Native 应用程序在使用 i18next 从应用程序更改时保存语言?

我在本机反应中使用i18next在应用程序中使用多种语言:用户可以通过单击此按钮中的按钮来更改应用程序中的语言我在AsyncStorage中设置语言,在我想要的 i18next 初始化文件中使用AsyncStorage的值,但它不会改变它,因为AsyncStorage它需要async和await,所以需要很长时间才能改变值,这是代码:

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import English from '../Translation/Languages/English.json';
import Spanish from '../Translation/Languages/Spanish.json';
import Arabic from '../Translation/Languages/Arabic.json';
import AsyncStorage from '@react-native-async-storage/async-storage';

let language = null;

const changeLanguage = async () => {
try {
    const Lang = await AsyncStorage.getItem('Language');
    if (Lang !== null) {
        language = Lang;
    }
}
catch (error) {
    console.log("Error ", error)
}
};

changeLanguage();

i18n
.use(initReactI18next)
.init({
    lng: language,
    fallbackLng: 'en',
    resources: {
        en: English, …
Run Code Online (Sandbox Code Playgroud)

localization i18next react-native asyncstorage react-i18next

2
推荐指数
1
解决办法
2363
查看次数

AsyncStorage说“未定义不是对象”

我正在处理购物清单,并希望将已检查的项目保存在本地存储中。但是,设置AsyncStorage并没有为我说undefined不是对象

这是我的代码:

import React from 'react';
import { StyleSheet, Dimensions } from 'react-native';
import { Container, Content, List, ListItem, Text, CheckBox, AsyncStorage } from 'native-base';

export default class ShoppingListItem extends React.Component {
  constructor(props) {
    super(props);
    this.saveCheck = this.saveCheck.bind(this)
    this.state = {
      checked: false
    }
  }

  render() {
    return (
      <ListItem style={styles.table}>
        <CheckBox 
          checked={this.state.checked}
          onPress={this.saveCheck}/>
        <Text>{this.props.item[0]} {this.props.item[1]} {this.props.item[2]}</Text>
      </ListItem>
    );
  }

  // componentWillMount () {
  //   AsyncStorage.getItem(this.props.key).then((value) => {
  //      this.setState({checked: value});
  //   }).done();
  // }

  saveCheck () {
    console.log(this.state.checked)
    this.setState({checked: …
Run Code Online (Sandbox Code Playgroud)

react-native asyncstorage

1
推荐指数
1
解决办法
3542
查看次数

React Native:await是一个保留字

我试图保存我的用户登录然后在他的管理页面上重定向他的事实但是当我使用await时我得到了这个错误:

await是一个保留字(101)

另一件事,一旦我的用户注册,我想将他发送到一个新的页面,而不是一个"幻灯片页面",我们可以用右上角的箭头返回.我怎样才能做到这一点?

这是我的代码:

 login(){
    firebase.auth().signInWithEmailAndPassword(this.state.emailLogin, 
    this.state.passwordLogin).then((user) =>  {
        // Send the user to the next step
        try {
            await AsyncStorage.setItem('@MySuperStore:key', 'I like 
            to save it.');
        } catch (error) {
            // Error saving data
        }
        const { navigate } = this.props.navigation; 
        navigate('Admin');
    }).catch(function(error) {
        var errorCode = error.code;
        var errorMessage = error.message;

        if (errorCode === 'auth/wrong-password') {
            alert('Wrong password.');
        } else {
            alert(errorMessage);         
        }
        console.log(error);
    });
}
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助.

react-native asyncstorage

1
推荐指数
1
解决办法
2767
查看次数

从AsyncStorage检索JWT以使用ApolloClient创建httpLink中间件的问题

我几乎从创建中间件Apollo GraphQL网络层文档中复制/粘贴示例代码,除了我从中拉动依赖项apollo-client-preset并且我使用一个简单的实用程序库来返回jwt标记.这是链接代码......

import { ApolloClient, HttpLink, InMemoryCache, ApolloLink, concat } from 'apollo-client-preset';
import { getSessionToken } from './api/localStorage';

const httpLink = new HttpLink({ uri: 'http://localhost:4000/' });

const authMiddleware = new ApolloLink((operation, forward) => {
  const token = getSessionToken();
  const authorizationHeader = token ? `Bearer ${token}` : null;

  operation.setContext({
    headers: {
      authorization: authorizationHeader,
    }
  });

  return forward(operation);
})

const client = new ApolloClient({
  link: concat(authMiddleware, httpLink),
});
Run Code Online (Sandbox Code Playgroud)

这是./api/localStorage播放中的方法......

export const getSessionToken = () …
Run Code Online (Sandbox Code Playgroud)

javascript async-await graphql react-native asyncstorage

1
推荐指数
1
解决办法
530
查看次数

React Native:如何在异步存储中存储对象

我想将对象存储在异步存储中,然后我要获取存储在对象中的不同值。

storeToken(responseData){

    AsyncStorage.setItem(ACCESS_TOKEN, responseData, (err)=> {
      if(err){
        console.log("an error");
        throw err;
      }
      console.log("success");
    }).catch((err)=> {
        console.log("error is: " + err);
    });
  }
Run Code Online (Sandbox Code Playgroud)

我已经以这种方式存储。但是在获取特定值时,我只能获取一个值,而无法从对象获取更多值。

react-native asyncstorage

1
推荐指数
1
解决办法
574
查看次数

TypeError:undefined不是对象(评估'_asyncStorage.AsyncStorage.setItem')

我正在这样导入AsyncSorage:

import { AsyncStorage } from '@react-native-community/async-storage'
Run Code Online (Sandbox Code Playgroud)

在我的代码中,我正在执行以下操作:

AsyncStorage.setItem('locale', locale)
Run Code Online (Sandbox Code Playgroud)

和这个:

AsyncStorage.getItem('user').then((value) => {
Run Code Online (Sandbox Code Playgroud)

它们都给我以下错误:

TypeError:undefined不是对象(评估'_asyncStorage.AsyncStorage.setItem')

TypeError:undefined不是对象(评估'_asyncStorage.AsyncStorage.getItem')

当我从'react-native'导入AsyncStorage时,没有问题,只是说应该从'@ react-native-community'导入AsyncStorage,因为不推荐使用'react-native'的AsyncStorage。

PS:我知道我应该await在AsyncStorage之前使用,但这不能解决问题。

我的@ react-native-community / async-storage版本如下: "@react-native-community/async-storage": "^1.6.1"

我已经尝试卸载并重新安装@ react-native-community / async-storage依赖项,但是没有用。

我已经尝试在它前面放一个等待,但这给了我同样的错误

我已经尝试过搜索它,但是没有找到任何解决方案。

该屏幕的完整代码:

import React from 'react';
import { View, Image, NativeModules } from 'react-native';
import { AsyncStorage } from '@react-native-community/async-storage'
import { styles } from '../../components/Styles.js';
import { GEOLocation } from '../../scripts/GEOLocation';
import Moment from 'moment/min/moment-with-locales';

export default class SplashScreen extends React.Component {


  constructor(props) {
    super(props);
    this.geo …
Run Code Online (Sandbox Code Playgroud)

react-native asyncstorage

1
推荐指数
1
解决办法
356
查看次数