adrianhajdin / social_media_app

Build a modern social app with a stunning UI with a native mobile feel, a special tech stack, an infinite scroll feature, and amazing performance using React JS, Appwrite, TypeScript, and more.
https://jsmastery.pro
1.07k stars 322 forks source link

cant create post #5

Open shreyzeous21 opened 8 months ago

shreyzeous21 commented 8 months ago

please fix with my problem am getting problem in creating the post

ChamomileTommy commented 8 months ago

maybe you did not set the permissions for storage, i tried and it worked (go to Storage ->Media->Settings->Permissions->any->choose alls->update )

hmongdev commented 8 months ago

I have a similar problem. My photo is stored, but is NOT posted in the database.

hmongdev commented 8 months ago

maybe you did not set the permissions for storage, i tried and it worked (go to Storage ->Media->Settings->Permissions->any->choose alls->update )

I've already done this.

Here is my issue: Screenshot 2023-11-02 190440

ChamomileTommy commented 8 months ago

maybe you did not set the permissions for storage, i tried and it worked (go to Storage ->Media->Settings->Permissions->any->choose alls->update )

I've already done this.

Here is my issue: Screenshot 2023-11-02 190440

please check you added attribute : imageUrl into Collection :Posts or not , check Spelling attribute : imageUrl (imageUrl , not ImageUrl )

hmongdev commented 8 months ago

Sorry, I solved the issue a while ago and just saw your message now. You're right.

I capitalized ImageUrl.

It was supposed to be imageUrl.

MohammadHarish52 commented 8 months ago

i am able to create a post but it is not coming in the posts database, there are no errors in the console and I am getting redirected also.

ChamomileTommy commented 8 months ago

i am able to create a post but it is not coming in the posts database, there are no errors in the console and I am getting redirected also.

Make sure you set permission for all collections, try it

MohammadHarish52 commented 8 months ago

i am able to create a post but it is not coming in the posts database, there are no errors in the console and I am getting redirected also.

Make sure you set permission for all collections, try it

my permissions are ok maybe that i haven't added the create post as of timestamp 3h 18 minutes i am adding all the functions in api.ts and letting you know then

ChamomileTommy commented 8 months ago

i am able to create a post but it is not coming in the posts database, there are no errors in the console and I am getting redirected also.

Make sure you set permission for all collections, try it

my permissions are ok maybe that i haven't added the create post as of timestamp 3h 18 minutes i am adding all the functions in api.ts and letting you know then

I did not fix any code, follow video and step by step with setup, I think the problem is not in api.ts

MohammadHarish52 commented 8 months ago

i am able to create a post but it is not coming in the posts database, there are no errors in the console and I am getting redirected also.

Make sure you set permission for all collections, try it

my permissions are ok maybe that i haven't added the create post as of timestamp 3h 18 minutes i am adding all the functions in api.ts and letting you know then

I did not fix any code, follow video and step by step with setup, I think the problem is not in api.ts

Sir i am talking about createpost and its related functions like upload file and alll

MohammadHarish52 commented 8 months ago

i am able to create a post but it is not coming in the posts database, there are no errors in the console and I am getting redirected also.

Make sure you set permission for all collections, try it

my permissions are ok maybe that i haven't added the create post as of timestamp 3h 18 minutes i am adding all the functions in api.ts and letting you know then

I did not fix any code, follow video and step by step with setup, I think the problem is not in api.ts

AppwriteException: Missing required parameter: "file" at Storage. (http://localhost:5173/node_modules/.vite/deps/appwrite.js?v=804954db:2713:15) at Generator.next () at http://localhost:5173/node_modules/.vite/deps/appwrite.js?v=804954db:503:67 at new Promise () at __awaiter (http://localhost:5173/node_modules/.vite/deps/appwrite.js?v=804954db:485:10) at Storage.createFile (http://localhost:5173/node_modules/.vite/deps/appwrite.js?v=804954db:2705:12) at uploadFile (http://localhost:5173/src/lib/appwrite/api.ts?t=1698993854245:117:39) at createPost (http://localhost:5173/src/lib/appwrite/api.ts?t=1698993854245:75:32) at Object.mutationFn (http://localhost:5173/src/lib/react-query/queriesAndMutations.ts?t=1698993854245:30:27) at Object.fn (http://localhost:5173/node_modules/.vite/deps/@tanstack_react-query.js?v=804954db:1097:31) api.ts:139 TypeError: Cannot read properties of undefined (reading '$id') at createPost (api.ts:110:49)

my code

import { INewPost, INewUser } from "@/types"; import { ID, Query } from "appwrite"; import { account, appwriteConfig, avatars, databases, storage } from "./config";

// Function to create a new user account export async function createUSerAccount(user: INewUser) { try { // Create a new account with Appwrite const newAccount = await account.create( ID.unique(), user.email, user.password, user.name );

if (!newAccount) throw Error;

// Generate an avatar URL for the user
const avatarUrl = avatars.getInitials(user.name);

// Save user information to the database
const newUser = await saveUserToDB({
  accountId: newAccount.$id,
  name: newAccount.name,
  email: newAccount.email,
  username: user.username,
  imageUrl: avatarUrl,
});

return newUser;

} catch (error) { console.log(error); return error; } }

// Function to save user information to the database export async function saveUserToDB(user: { email: string; name: string; accountId: string; imageUrl: URL; username?: string; }) { try { const newUser = await databases.createDocument( appwriteConfig.databaseId, appwriteConfig.userCollectionId, ID.unique(), user ); return newUser; } catch (error) { console.log(error); } }

// Function to sign in with an email and password export async function signInAccount(user: { email: string; password: string }) { try { // Create a session for the user const session = await account.createEmailSession(user.email, user.password); return session; } catch (error) { console.log(error); } }

// Function to get the current user's information export async function getCurrentUser() { try { // Get the current account information from Appwrite const currentAccount = await account.get();

if (!currentAccount) throw error;

// Query the database for the user's information using the account ID
const currentUser = await databases.listDocuments(
  appwriteConfig.databaseId,
  appwriteConfig.userCollectionId,
  [Query.equal("accountId", currentAccount.$id)]
);

if (!currentUser) throw Error;
return currentUser.documents[0];

} catch (error) { console.log(error); } }

export async function signOutAccount() { try { // Delete the current session const session = await account.deleteSession("current"); return session; } catch (error) { console.log(error); } }

export async function createPost(post: INewPost) { try { //upload image to storage const uploadedFile = await uploadFile(post.file[0]);

if (!uploadFile) {
  throw Error;
}
// Get file url
const fileUrl = getFilePreview(uploadedFile.$id);

if (!fileUrl) {
  deleteFile(uploadedFile.$id);
  throw Error;
}
// convert tags into an array
const tags = post.tags?.replace(/ /g, "").split(",") || [];

// save post to database
const newPost = await databases.createDocument(
  appwriteConfig.databaseId,
  appwriteConfig.postsCollectionId,
  ID.unique(),
  {
    creator: post.userId,
    caption: post.caption,
    location: post.location,
    imageUrl: fileUrl,
    imageId: uploadedFile?.$id,
    tags: tags,
  }
);
if (!newPost) {
  await deleteFile(uploadedFile?.$id);
  throw Error;
}
return newPost;

} catch (error) { console.log(error); } }

export async function deleteFile(fileId: string) { try { await storage.deleteFile(appwriteConfig.storageId, fileId); return { status: "ok" }; } catch (error) { console.log(error); } }

export async function uploadFile(file: File) { try { const uploadFile = await storage.createFile( appwriteConfig.storageId, ID.unique(), file ); return uploadFile; } catch (error) { console.log(error); } }

export async function getFilePreview(fileId: string) { try { const fileUrl = storage.getFilePreview( appwriteConfig.storageId, fileId, 2000, 2000, "top", 100 ); return fileUrl; } catch (error) { console.log(error); } }

ChamomileTommy commented 7 months ago

you chose file before you click button create post or not ?

ChamomileTommy commented 7 months ago

i used author's code and it s no problem , you re coding yourself ?

MohammadHarish52 commented 7 months ago

i used author's code and it s no problem , you re coding yourself ?

There was typo in passing props in The section of Add photos were i gave the name to Add photos instead it should be file . Took me a long to realised but its fixed now

MohammadHarish52 commented 7 months ago

maybe you did not set the permissions for storage, i tried and it worked (go to Storage ->Media->Settings->Permissions->any->choose alls->update )

I've already done this.

Here is my issue: Screenshot 2023-11-02 190440

There is a typo It should be "imageUrl"

abhijitpati11 commented 6 months ago

Screenshot (160)

Code snippet : errorCode

please help with this error, everything seems correct from my side..

ajayvish0 commented 5 months ago

Our fileUrl is getting an promise. so we have to only add await in front of getFilePreview(uploadedFile.$id); Screenshot 2024-01-19 135949

Viraj-Pr commented 5 months ago

AppwriteException: Invalid document structure: Attribute "imageUrl" has invalid format. Value must be a valid URL at Client. (http://localhost:5173/node_modules/.vite/deps/appwrite.js?v=1b745d41:850:17) at Generator.next () at fulfilled (http://localhost:5173/node_modules/.vite/deps/appwrite.js?v=1b745d41:488:24)

this error is coming and i can't create post but image is being stored.