Hi, I have a question:
I am trying to assign the currently authenticated user to the new record created in a collection - however I am always getting the errors. (When i change the arrows from → to ~> nothing changes)
violation for ‘existence’:
- expected: ‘existence’ of type ‘mandatory’
- but found: ‘existence’ of type ‘optional’
'Requests': collection ['Request ID'] {
'Request ID': text @default: auto-increment
'User': text -> ^ ^ .'Users'[] @default: user .'ID'
}
'Create new': command {
'Nr': text
} => update .'Requests' = create (
'Request ID' = @ .'Nr'
'User' = user >'ID' ### I have tried multiple different solutions, using only user, user .’ID’, however none of them work.
)
Commands are executed by the datastore. They may be invoked by application users or by external systems (via an Alan interface). In the latter case, a user is not available (existence is optional).
To solve the problem, you have multiple different options:
use an action instead of a command. An action is executed by the webclient instead of by the datastore:
'Create new': action { ...
switch on the existence of user and handle the case where user is not available:
/* set a Source that creates the Request: Unknown if no user available */
'Create new': command {
'Nr': text
} => update .'Requests' = create (
'Request ID' = @ .'Nr'
'Source' = switch user (
| node as $'usr' = 'User' ( 'ID' = $'usr'.'ID' )
| none => 'Unknown' ( )
)
)
/* only create a new item when user is available */
'Create new': command {
'Nr': text
} => switch user (
| node as $'usr' => update .'Requests' = create (
'Request ID' = @ .'Nr'
'User' = $'usr'.'ID'
)
| none => ignore
)