Closed elkee2003 closed 1 year ago
So apparently there is something new called 'Lazy loading & nested query predicates for AWS Amplify DataStore'. It is the change of the syntax from: Before: DataStore.query(Post, p => p.id(‘eq’, ‘123’)) Now: DataStore.query(Post, p => p.id.eq(‘123’))
So, in my case, it is:
useEffect(()=>{ DataStore.query(User, (user)=>user.sub.eq(sub)).then((users)=>setDbUser(users[0])) }, [sub])
Hi @elkee2003 👋 yes, the DataStore predicate syntax has changed between v4 and v5 of the JS library. Has this resolved the issue for you?
Hi @elkee2003 👋 yes, the DataStore predicate syntax has changed between v4 and v5 of the JS library. Has this resolved the issue for you?
Yes that problem has been solved. However, I am trying to update a user and it is giving me an error. I believe the error is coming from the syntax. Could you please help me pin point it out? I have uploaded the error message and the code photos
`const ProfileScreen = () => {
const {sub, dbUser, setDbUser} = useAuthContext()
const [name, setName] = useState(dbUser?.name || "")
const [address, setAddress] = useState(dbUser?.address || "")
const [lat, setLat] = useState(dbUser?.lat + "" || "0")
const [lng, setLng] = useState (dbUser?.lng + "" || "0")
const onSave = async ()=>{
if (dbUser){
await updateUser()
}else{
await createUser()
}
// Update User function
const updateUser = async ()=>{
const user = await DataStore.save(
User.copyOf(dbUser, (updated)=>{
updated.name = name;
updated.address = address;
updated.lat = parseFloat(lat);
updated.lng = parseFloat(lng);
})
);
setDbUser(user)
};
// Create User function
const createUser = async () =>{
try{
const user = await DataStore.save(new User({
name,
address,
lat:parseFloat(lat),
lng:parseFloat(lng), sub
}))
console.log(user)
setDbUser(user)
}catch(e){
Alert.alert("Error", e.message)
}
};
}`
@elkee2003 awesome, glad you were able to resolve the issue!
This new error doesn't sound like it's coming from DataStore. "undefined is not a function", to me, sounds like an undefined function is attempting to be called. It might be the way updateUser
and createUser
are being called before they are defined below.
I would try either changing them to normal functions rather than arrow functions so they are hoisted or simply move the if statement below the function declarations.
example:
const onSave = async ()=>{
if (dbUser){
await updateUser()
}else{
await createUser()
}
// Update User function
async function updateUser() {
const user = await DataStore.save(
User.copyOf(dbUser, (updated)=>{
updated.name = name;
updated.address = address;
updated.lat = parseFloat(lat);
updated.lng = parseFloat(lng);
})
);
setDbUser(user)
};
// Create User function
async function createUser() {
try{
const user = await DataStore.save(new User({
name,
address,
lat:parseFloat(lat),
lng:parseFloat(lng), sub
}))
console.log(user)
setDbUser(user)
}catch(e){
Alert.alert("Error", e.message)
}
};
}`
hello i face the same issue and am unable to solve same like above 1st one is in useeffect and 2nd in update user i follow your suggestion but unable to solve
async function updateUser() { const user = await DataStore.save( User.copyOf(dbUser , (updated) => { updated.name = name; updated.address = address; updated.lat = parseFloat(lat); updated.lng = parseFloat(lng); }) ); setdbUser(user); };
DataStore.query(User, (user) => user.sub.eq(sub)).then(setdbUser) }, [sub])
ERROR [ERROR] 41:09.978 DataStore - The source object is not a valid model {"source": []} WARN Possible Unhandled Promise Rejection (id: 1): Error: The source object is not a valid model Error: The source object is not a valid model
Before opening, please confirm:
JavaScript Framework
React Native
Amplify APIs
DataStore
Amplify Categories
storage
Environment information
Describe the bug
I try fetching my data from DataStore to query it but it keeps giving me this error: P Possible unhandled promise rejection (): typeerror: object is not a function.
Expected behavior
It is meant to successfully query my data, because I went back to check in the models and User is an object
Reproduction steps
Code Snippet
import { createContext, useState,useEffect, useContext } from "react"; import {Auth, DataStore} from 'aws-amplify'; import { User } from "../models";
const AuthContext = createContext({})
const AuthContextProvider = ({children})=>{ const [authUser, setAuthUser] = useState(null) const [dbUser, setDbUser] = useState(null) const sub = authUser?.attributes?.sub;
}
export default AuthContextProvider;
export const useAuthContext = ()=> useContext(AuthContext)
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
Tecno Camon 19
Mobile Operating System
Android 12
Mobile Browser
Chrome
Mobile Browser Version
No response
Additional information and screenshots
No response