Skip to content

message.answer(values) ​

Answers a question (a message that carries a form) with the values chosen by the person.

answer(values: Record<string, unknown>): Promise<SendResult>
js
const result = await message.answer({ direction: 'left' });

answer is a method of the message (not of the world) and only a message with a form has it.

Parameters ​

ParameterWhat it is
valuesAn object keyed by the name of the fields in message.form.fields.

The value of a field depends on its type:

Field typeValue
'select'The value of the chosen option as a string. A field that allows more than one option (max above 1) takes an array of values.
'text', 'textarea'A string.
'number', 'integer'A number.
'bool'true or false.
'date'A date as '2026-11-05'.

Returns ​

Like send, a Promise that resolves to a SendResult:

ResultWhat it means
'sent'The answer was delivered.
'failed'Sent but not delivered. The answer stays in messages and its retry() sends it again.
'not-allowed'Nothing was sent. The question is already answered or no longer among the messages, no key of values names a field of the form or canSend is false.

Usage ​

Find the open questions ​

A question has a form and stays open while isAnswered is false:

js
world.subscribe((node) => {
  const open = node.messages.filter((message) => message.form && !message.isAnswered);
  drawQuestions(open);
});

Answer from a form you draw ​

Render one control per field and pass all the values to a single answer call:

js
function drawQuestion(message) {
  const field = message.form.fields[0]; // e.g. { name: 'direction', type: 'select', options: [...] }
  const select = document.createElement('select');
  for (const option of field.options) select.add(new Option(option.label, option.value));
  const button = document.createElement('button');
  button.textContent = 'Answer';
  button.onclick = () => message.answer({ [field.name]: select.value });
  return [select, button];
}

Even when the sender wrote plain strings, the options of a 'select' field always arrive in a message as { value, label } objects.

What follows ​

The answer becomes a new message of the person. It carries values, a readable text ("direction: left") and the id of the question in answers. In the same update of the world the question gets isAnswered: true. Render both in subscribe.

Caveats ​

  • Once per question. After the first answer isAnswered is true and every later answer resolves 'not-allowed'.
  • It is a message. An answer is sent like any other message and needs canSend. The person sees it as their own answer in the UNaIVERSE view of the world too.
  • It works on a message from an older node. The call identifies the message by its id. You can therefore answer a message that your subscribe listener received in an earlier call.
  • Pass a value of the right type for at least one field of the form. Keys that name no field of the form are dropped. The receiver sees a field as not answered in two cases: (1) its value has the wrong type or breaks a limit of the field (min, max, maxLength, format) and is dropped or (2) values leaves it out (leaving a field out is allowed).
  • The questions of the person (sent with send({ text, form })) have answer too. They are meant for the receiver to answer.

Troubleshooting ​

CodeWhyWhat to do
'failed'The webapp could not run the call (rare).Render the newest messages and let the person try again.

See also ​

The UNaIVERSE GUI kit, released under the Apache License 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.