I am developing react native app with Amplify and using DataStore. I want to save the user token to DB, so I checked and saved in App.js.
During testing, I noticed that the token often doesn't update. So I tested the token alternately to two different simulators.
Belows are my code
useEffect(() => {
const updateDeviceToken = async () => {
console.log('Entering updateDeviceToken');
if (deviceToken) {
console.log('deviceToken is present');
if (dbUser && deviceToken) {
if (dbUser && dbUser.token !== deviceToken) {
console.log('auth', deviceToken);
console.log('dbUser.token', dbUser.token);
console.log('dbUser.id', dbUser.id);
try {
const newUser = await DataStore.query(User, user =>
user.id.eq(dbUser.id),
);
console.log('user1', newUser[0]);
const dbuser = await DataStore.save(
User.copyOf(newUser[0], updated => {
updated.token = deviceToken;
}),
);
console.log('user2', dbuser);
setDbUser(dbuser);
} catch (err) {
console.err('update token error', err);
}
}
} else {
console.log('dbUser.token is the same as deviceToken');
}
} else {
console.log('deviceToken is not present');
}
};
The output log is as follows
LOG Entering updateDeviceToken
LOG deviceToken is present
LOG auth tokenaaaaa
LOG dbUser.token tokenbbbbb
LOG dbUser.id 73e114d0-6c73-40d0-9b18-c015ae9edd8d
LOG user1 {....."token": "tokenbbbbb",.....}
LOG user2 {....."token": "tokenaaaaa",.....}
On the log, the update appears to be normal. But when I check the actual data on the amply console, it doesn't update and still shows 'tokenbbbbbb'.
I really don't know what's the cause.
If anyone has solutions, I'd greatly appreciate detailed explanations.