Code not creating new posts in Amity dashboard

When I run this code, there are no new posts created in my Amity dashboard. Am I missing something?

<script>
  import { PostRepository, FileRepository, PostTargetType } from '@amityco/js-sdk';
  import AmityClient from '@amityco/js-sdk';

  const client = new AmityClient({
    apiKey: {API_KEY},
    apiRegion: "US",
  });

  // Function to upload file to Amity
  const uploadFile = async () => {
    // Get file object from the HTML form
    const fileObject = document.getElementsByName("file")[0].files[0];

    // Create the file on Amity
    const amityFile = await createFile(fileObject);

    // Create the image post with the uploaded file
    await createImagePost(amityFile.fileId);

    // Reset the form
    document.getElementsByTagName("form")[0].reset();
  };

  // Function to create a file on Amity
  const createFile = async (fileObject) => {
    const liveFile = FileRepository.createFile({ file: fileObject });
    const model = await new Promise((resolve) =>
      liveFile.once("dataUpdated", (data) => resolve(data))
    );
    return model;
  };

  // Function to create an image post on Amity
  const createImagePost = async (fileId) => {
    await PostRepository.createImagePost({
      targetId: "user1", 
      targetType: PostTargetType.CommunityFeed, // PostTargetType.CommunityFeed for community feed or PostTargetType.UserFeed for user profile feed
      tags: ["a", "b"], 
      imageIds: [fileId], 
    });
  };
</script>

<!-- HTML form to upload a file -->
<form enctype="multipart/form-data">
  <input type="file" name="file">
  <button type="submit" on:click|preventDefault={uploadFile}>Upload</button>
</form>

hi @unicoder

  • may we confirm which JS-SDK version you’re using?
  • based on your code, for target-id can you make sure to use communityID (sample below), let us know if this works
// Function to create an image post on Amity
const createImagePost = async (fileId) => {
await PostRepository.createImagePost({
targetId: "user1", // community id or user id
targetType: PostTargetType.CommunityFeed, // PostTargetType.CommunityFeed for community feed or PostTargetType.UserFeed for user profile feed
tags: ["a", "b"], // optional, max no. of tags = 5, max length per tag = 24 chars
imageIds: [fileId], // max: 10 image of fileid that model above.
});
};

Thank you

@amitysupport Thank you for your reply.

“name”: “@amityco/js-sdk”,
“version”: “5.34.0”,

I have swapped out the user id with the community id, and the posts are still not being registered in the community feed. Is there some initialization step that seems to be missing?

@unicoder

can you please help check if you finished registering AmityClient session (aka login)?

client.registerSession({
    userId: uid,
    displayName: name,
});
client.on(
    "connectionStatusChanged",
    ({ oldValue, newValue }) => {
      if (newValue === "connected") {
        // Try to create post
      }
    }
  );
};

Thank you