Skip to content
Back

[Solved] Set current date to DateTime with function

  • 0
  • Self Hosted
  • Tools
LDs_Group
13 Sep, 2023, 17:41

Hi, I'm happy to discouver just a few moment how to initialize database with function. 🙂

I occured a dfficult: How do I set default date in DateTime field with function

TypeScript
databases.createDatetimeAttribute(
  prodDb.$id,
  contractColl.$id,
  'start_date',
  true,
  Date.now().toLocaleString()
);

throw this error

TypeScript
AppwriteException [Error]: Invalid `default` param: DateTime::__construct(): Failed to parse time string (1,694,626,509,410) at position 0 (1): Unexpected character
    at Client.call (/usr/local/server/src/function/node_modules/node-appwrite/lib/client.js:172:31)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Databases.createDatetimeAttribute (/usr/local/server/src/function/node_modules/node-appwrite/lib/services/databases.js:528:16) {
  code: 400,
  type: 'general_argument_invalid',
  response: {
    message: 'Invalid `default` param: DateTime::__construct(): Failed to parse time string (1,694,626,509,410) at position 0 (1): Unexpected character',
    code: 400,
    type: 'general_argument_invalid',
    version: '1.4.2'
  }
}
TL;DR
User is discussing setting permissions for users in a collection and encountering an error when trying to set a default date in a DateTime field using a function. They receive an error message indicating an invalid default parameter. Solution: To set the current date as the default value for the DateTime field with a function, use the following code: ```js databases.createDatetimeAttribute( prodDb.$id, contractColl.$id, 'start_date', true, (new Date()).toISOString() ); ``` This should resolve the error.
Binyamin
13 Sep, 2023, 17:49

Use this instead

TypeScript
databases.createDatetimeAttribute(
  prodDb.$id,
  contractColl.$id,
  'start_date',
  true,
  (new Date()).toISOString()
);
LDs_Group
13 Sep, 2023, 17:49

It's generate the same error

Binyamin
13 Sep, 2023, 17:50

The message should be different Can you share it?

LDs_Group
13 Sep, 2023, 17:50

Let me take my computer

Binyamin
13 Sep, 2023, 17:52

There's another thing you should check is that the default can't be set when the field is required https://appwrite.io/docs/server/databases?sdk=nodejs-default#databasesCreateDatetimeAttribute

LDs_Group
13 Sep, 2023, 17:54
LDs_Group
13 Sep, 2023, 17:55
LDs_Group
13 Sep, 2023, 17:55

just now

Binyamin
13 Sep, 2023, 17:55

Yes, Now is the second error

Binyamin
13 Sep, 2023, 17:56

Either change the 4th parameter to false

Binyamin
13 Sep, 2023, 17:56

Or remove the default

Binyamin
13 Sep, 2023, 17:56

It seems like you can't set both

LDs_Group
13 Sep, 2023, 17:57

let try

LDs_Group
13 Sep, 2023, 17:58

You are strong, it's work

LDs_Group
13 Sep, 2023, 18:00

I've the same error with enum now. So I want to know:

When we set default value, we always set required to false ?*

Binyamin
13 Sep, 2023, 18:01

Yes, Think of it that way if the user must enter a value the default value would never be in use

LDs_Group
13 Sep, 2023, 18:06

It's correct, I edit my code. Now all work without warning

LDs_Group
13 Sep, 2023, 18:07

It's my first quick mistake solved 🙂

LDs_Group
13 Sep, 2023, 18:08

[Solved] Set current date to DateTime with function

LDs_Group
13 Sep, 2023, 18:10

@Binyamin , I will open new issue for explain if you are for explication

Binyamin
13 Sep, 2023, 18:10

On what part?

LDs_Group
13 Sep, 2023, 18:11

difference between set permission to collection or to a query (Permission and Document Security)

Binyamin
13 Sep, 2023, 18:13

Is this a new thing? When setting permission to a collection only then each user request to that collection will be check against the collection permission table But if you also add document permission then it will check against both of them And if **one **of them is good for the current user then the that user will get access to that document in the collection.

LDs_Group
13 Sep, 2023, 18:15

That is my case, 'ADMIN (one person)' are king, He can do anything but user can edit only the files that related to their company

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