1

I have this line in my react native app to stock a variable in AsyncStorage:

AsyncStorage.setItem('userId',theuser.id);

but when I run the app it gives me an error:

TypeError: expected dynamic type string', but had typeint64 react native

It works fine when the variable I want to pass is string but I can't pass any object or int variable?

8
  • Is the id a number? It has to be a string. Commented May 8, 2017 at 22:40
  • @AndrewLi it has nothing to do with the type of the id I want pass the age for example but It won't It accepts only string Commented May 8, 2017 at 22:41
  • What do you mean it has nothing to do with type? The problem is the type -- the value must be a string. Commented May 8, 2017 at 22:42
  • @AndrewLi it has nothing to do with the type of the ID, even this "AsyncStorage.setItem('number',15);" will get the same error. Commented May 8, 2017 at 22:44
  • It has all to do with the type! In your counterexample, the second argument is still a number! It can't be a number! That's what the error is saying! Commented May 8, 2017 at 22:45

1 Answer 1

4

In the comments above you said you would want to pass a whole object, you would do it like this:

AsyncStorage.setItem('userId', JSON.stringify(theuser));
Sign up to request clarification or add additional context in comments.

2 Comments

but then it gives me this error ( JSON Parse error: Unexpected EOF ) when I try to use the field userId.id or userId.department in the app.
How are you parsing it? Here's an example of what to do: AsyncStorage.getItem('userId', (err, user) => this.setState({ user: JSON.parse(user) }));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.