Back

Error in appwrite JSDelivr SDK

  • 0
  • Databases
  • Web
  • Cloud
Ryan
22 Dec, 2023, 15:33

my code: (message.txt)

my error:

TypeScript
sdk.js:3128 Uncaught TypeError: Cannot read properties of undefined (reading 'Appwrite')
    at sdk.js:3128:25
TL;DR
User is encountering a 500 Internal Server Error when using the Appwrite Python SDK. They have provided their code and the error traceback. They are also encountering a 401 Unauthorized error while trying to fetch messages. User also mentions another error related to an undefined property 'Appwrite' in the SDK. Solution: - For the 500 Internal Server Error, there is no specific solution mentioned. User intends to investigate further. - For the 401 Unauthorized error, the error message suggests that the current user is not authorized to perform the action. User may need to check the permissions of the collection or ensure they have a session created. -
Ryan
22 Dec, 2023, 16:29

the line sdk.js:3128:

TypeScript
})(this.Appwrite = this.Appwrite || {}, null, window);
Drake
22 Dec, 2023, 17:27

You don't need the import. Instead of new appwrite.Client() would you please try new Appwrite.Client()?

Ryan
23 Dec, 2023, 06:32

let me try that.

Ryan
23 Dec, 2023, 06:38

I think it works. the database init var would also be Appwrite.Database();? It is js const client = new appwrite.Client() const database = new Appwrite.Databases(client, "chat-msgs");

Ryan
23 Dec, 2023, 07:06

Now, New Problem:

TypeScript
ET https://cloud.appwrite.io/v1/databases/chat-msgs/collections/chat-msgs-collection/documents 401
(anonymous) @ sdk.js:381
(anonymous) @ sdk.js:25
n @ sdk.js:21
call @ sdk.js:341
(anonymous) @ sdk.js:1713
(anonymous) @ sdk.js:25
n @ sdk.js:21
listDocuments @ sdk.js:1700
fetchMessages @ (index):136
(anonymous) @ (index):156
(index):151 Error fetching messages from Cloud:  AppwriteException: The current user is not authorized to perform the requested action.
    at e.Client.<anonymous> (https://cdn.jsdelivr.net/npm/appwrite@13.0.1:7:19529)
    at Generator.next (<anonymous>)
    at r (https://cdn.jsdelivr.net/npm/appwrite@13.0.1:7:115)

AKA:

TypeScript
GET https://cloud.appwrite.io/v1/databases/chat-msgs/collections/chat-msgs-collection/documents

Error fetching messages from Cloud:  AppwriteException: The current user is not authorized to perform the requested action.
Ryan
23 Dec, 2023, 07:09

My Code:

TypeScript
// Function to add a message to the list with timestamp
function addMessage(message) {
    // Shortening it bc msg too long
  }

var socket = io();

// Initialize the AppWrite client
const client = new Appwrite.Client();


client.setEndpoint('https://cloud.appwrite.io/v1'); // Your Appwrite Endpoint
client.setProject('privatetable-chat'); // Your project ID
   

// Initialize Database service
const database = new Appwrite.Databases(client, "chat-msgs");


// Event listener for form submission
document.getElementById('form').addEventListener('submit', function (e) {
    // More shortening...
});

// Event listener for receiving messages
socket.on('message', function (message) {
    addMessage(JSON.parse(message));
});

// Function to fetch messages from AppWrite
function fetchMessages() {
  const response = database.listDocuments("chat-msgs", "chat-msgs-collection")
    .then(function () {
      console.log(response)
      var messagesList = document.getElementById('messages');
      messagesList.innerHTML = ''; // Clear existing messages

      // Add each message to the list
      response.documents.forEach(function (document) {
        addMessage({
          timestamp: timestamp,
          data: message
        });
      });
    })
    .catch(function (error) {
      console.error('Error fetching messages from Cloud: ', error);
    });
}

// Call fetchMessages when the page loads to load existing messages
fetchMessages();
ideclon
24 Dec, 2023, 17:58

I don’t see you creating a Session anywhere here. Does you Collection have read permissions form All guests?

Ryan
25 Dec, 2023, 09:52

I don't know, and I don't know how. Please guide me

Drake
25 Dec, 2023, 17:05
Drake
25 Dec, 2023, 17:07
Ryan
26 Dec, 2023, 10:01

Ok, I'll see this and get back to you.

Ryan
26 Dec, 2023, 14:29

Umm. The Appwrite Python SDK gives this error:

TypeScript
Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\site-packages\appwrite\client.py", line 100, in call
    response.raise_for_status()
  File "C:\Program Files\Python38\lib\site-packages\requests\models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: https://cloud.appwrite.io/v1/databases/chat-msgs/collections/chat-msgs-collection/documents

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\site-packages\eventlet\hubs\hub.py", line 473, in fire_timers
    timer()
  File "C:\Program Files\Python38\lib\site-packages\eventlet\hubs\timer.py", line 59, in __call__
    cb(*args, **kw)
  File "C:\Program Files\Python38\lib\site-packages\eventlet\greenthread.py", line 222, in main
    result = function(*args, **kwargs)
  File "C:\Program Files\Python38\lib\site-packages\socketio\server.py", line 584, in _handle_event_internal
    r = server._trigger_event(data[0], namespace, sid, *data[1:])
  File "C:\Program Files\Python38\lib\site-packages\socketio\server.py", line 609, in _trigger_event
    return self.handlers[namespace][event](*args)
  File "script.py", line 67, in chat_message
    document = database.create_document('chat-msgs','chat-msgs-collection', str(uuid.uuid4()), {'timestamp': timestamp, 'message': data})
  File "C:\Program Files\Python38\lib\site-packages\appwrite\services\databases.py", line 945, in create_document
    return self.client.call('post', api_path, {
  File "C:\Program Files\Python38\lib\site-packages\appwrite\client.py", line 112, in call
    raise AppwriteException(response.json()['message'], response.status_code, response.json().get('type'), response.json())
appwrite.exception.AppwriteException: Server Error

So, basically 500 Server Error from Appwrite's API...

Ryan
26 Dec, 2023, 14:32

if you want to see my full code, visit the GitHub

ideclon
26 Dec, 2023, 16:03

Firstly, you really shouldn’t be posting your API key publicly on the Internet

Ryan
26 Dec, 2023, 16:04

yeah, i know, I'll regenerate new one once everythings done

Ryan
26 Dec, 2023, 16:04

plus, it only has access to db

ideclon
26 Dec, 2023, 16:09

Best not to get into the habit of it!

ideclon
26 Dec, 2023, 16:09

Secondly, have you tried using Appwrite’s built in generator for your document ID? (Use the string ”unique()”

Ryan
26 Dec, 2023, 16:10

Yeah, I initially put this repo to private, but since I am too lazy to provide the full code each time, I set it to public

Ryan
26 Dec, 2023, 16:10

idk, I'll try it

ideclon
26 Dec, 2023, 16:12

Still, it’s best to never put secrets in Git repositories.

ideclon
26 Dec, 2023, 16:13

Always remember, down the line, even if you delete it, it’s still out there!

Ryan
26 Dec, 2023, 16:13

I deactivated it

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more