#Invalid UserId in react
44 messages · Page 1 of 1 (latest)
try{
return await this.databases.createDocument(
conf.appwriteDatabaseId,
conf.appwriteCollectionId,
slug,
{
title,
content,
featuredImage,
status,
userId,
}
)
}
catch(error){
console.log("Appwrite serive :: createPost :: error", error);
}
}```
@raven cosmos @gray pagoda
@polar python
Please, avoid pinging core members if it's not necessarily 😅
okay
Looks like userId is required, but you're not specifying it?
Also, do you can see the data being saved in the console at all?
I think that it doesn't have a proper structure (Json-like)
It's not that it never happens, sometimes it gets posted
Hmm then maybe you're not specifying the user ID?
Anyways, I recommend doing that in a function to prevent manipulation
How to specify the user ID
?
You have here an userId attribute
yup
i am so frustrated
It is not getting solved
see
use ID.unique
const newAccount = await account.create(
ID.unique(),
user.email,
user.password,
user.name
);
like this
you are using account api, OP is using this.databases.createDocument, totally unrelated.
make sure that the value of userId is valid. do a console.log before firing the api.
const newUser = await databases.createDocument(
appwriteConfig.databaseId,
appwriteConfig.userCollectionId,
ID.unique(),
user
);
is this the code for it?
yep this is for saving in the database or collection that is for storing in the users
sorry ig i understood it wrong then! sorry guys!
?
userId in createPost: 66e07400002d6bb9e23e , i have console.log user id which is valid but some times show user id is undefined
if its undefined, you know the id isn't valid. in that case the value is obviously not accepted by the sdk/backend.
then what should i do now?
suggest me solution
@deep mulch
how are you passing the userId, whats the value of it? current signed in user's id?
yup id is currently signed user , Whichever user logs in, I store it in the state of Redux
the id might've been reset, override or something, how are you adding it? assuming await account.get() then getting $id?
why not do an on demand account.get() and fetch id?
async getCurrentUser(){
try{
return await this.account.get();
}
catch(err){
return null;
// console.log("Appwrite service::getCurrentUser::error",err );
}
return null;
}
const userData = useSelector(state => state.auth.userData);
if (file) {
const fileId = file.$id
data.featuredImage = fileId
const dbPost = await appwriteService.createPost({
...data,
userId: userData?.$id,
})``` in postform when i post form
yeah. there could be a chance why this might be null, network issues, auth issues, etc. this is something client side, you might need to figure how to either get the id or move along with invalid id and not allow doc operations.
this isn't really an appwrite related issue.
Now I have removed return null but still the same error is being shown and I had installed this because after logging out, another error of appwrite comes 'getCurrentUser missing scope 401'
if you logout, account.get() will throw an error.
yup or the userid error still same
to avoid this error i had used return null in catch block