Back

[SOLVED] Can't create a new Document , Giving error as 'Value must be a valid JSON string'

  • 1
  • Databases
  • General
  • Web
Sam
27 May, 2023, 11:10

still no luck!

TL;DR
Title: [SOLVED] Can't create a new Document, error: 'Value must be a valid JSON string' Solution: The issue was with the code not passing the `database_id` when calling `database`. Update the code to include `const databases = new Databases(client, "DB_ID")` and the error should be resolved.
Bouahaza
27 May, 2023, 11:14

You didn't set database ?

Sam
27 May, 2023, 11:16
Sam
27 May, 2023, 11:17

I created this db!

Bouahaza
27 May, 2023, 11:18

So you need to set ID here

Sam
27 May, 2023, 11:18

yes I did

Sam
27 May, 2023, 11:19
TypeScript
    e.preventDefault();
    const promise = await databases.createDocument(
      "6471bb9750ced865202e",
      "6471bbb56256066bc8e1",
      uuidv4(),
      {
        todo,
      }
    );
    console.log(promise);
    promise.then(
      function (response) {
        console.log(response);
      },
      function (error) {
        console.log(error);
      }
    );
    //window.location.reload() // handle it in different way
    e.target.reset();
    // window.location.reload();
  };
Bouahaza
27 May, 2023, 11:20

Check your code cause somethings wrong with the url called. There should be no undefined here

Sam
27 May, 2023, 11:21

is my config file correct?

Sam
27 May, 2023, 11:21

I didn't find anything wrong in my code!

Sam
27 May, 2023, 11:21

User is logging in and out to the system

Sam
27 May, 2023, 11:21

only Database has issue

Sam
27 May, 2023, 11:22

This code is same as the documentation

Bouahaza
27 May, 2023, 11:26

Isn't your code crash after ? You use await AND .then on promise already resolved

Bouahaza
27 May, 2023, 11:28

For now I've no idea, just know that you need to fix "undefined"

Sam
27 May, 2023, 11:28

I am new to appwrite. so I dont understand why this behaves like this

Bouahaza
27 May, 2023, 11:30

Someone else will answer you later 🙂 Keep focus

Sam
27 May, 2023, 11:35

Hey

Sam
27 May, 2023, 11:35

I fixed the issue

Sam
27 May, 2023, 11:35

<:appwritebow:824077772192088064>

Sam
27 May, 2023, 11:38

Basically We need to pass the database_id in when we call database like this const databases = new Databases(client, "DB_ID")

Sam
27 May, 2023, 11:39
TypeScript
      "COLLECTION_ID",
      uuidv4(),
      {
        todo,
      }
    );```
Sam
27 May, 2023, 11:40

This is the modified code to create a document, same is applicable for all CRUD operations

Bouahaza
27 May, 2023, 12:05

Great that you find a solution. Don't forget to add [SOLVED] at begin of your post title

Sam
27 May, 2023, 12:15

[SOLVED] Can't create a new Document , Giving error as 'Value must be a valid JSON string'

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