Chat / JavaScript
Chat JavaScript v4
Chat JavaScript
Chat
JavaScript
Home
/
Chat
/
JavaScript
/
User
This is the new Docs for Chat SDK v4 for JavaScript. To see the previous Docs, click here.

Manage user metadata

Metadata consists of key-value items in which you can store additional information to users. You can add up to five key-value items for user metadata. Each key can have up to 128 characters and value can have up to 190 characters as string. This section explains how to manage user metadata.


Create metadata

You can create additional information such as phone number, email address or other descriptions to a user, which can be fetched or rendered into the UI. As an object, user metadata is stored into a User object.

To store user metadata into a User object, create Dictionary of key-value items, and then pass it as an argument to a parameter when calling the createMetaData() method. You can add multiple key-value items in the dictionary.

const data: MetaData = {
    key1: 'value1',
    key2: 'value2'
};
await sb.currentUser.createMetaData(data);

Retrieve

You can retrieve metadata stored to a user by calling the metadata property of a User object.

const value1 = user.metaData['key1'];

Update

You can update metadata of a user by adding Dictionary of key-value items, and then pass it as an argument to a parameter when calling the updateMetaData() method. Values of existing keys will be updated and values of new keys will be added if the upsert parameter is set to true. You can put multiple key-value items in the dictionary.

const data: MetaData = {
    key1: 'valueToUpdate1', // Update an existing item with a new value.
    key2: 'valueToUpdate2', // Update an existing item with a new value.
};
await sb.currentUser.updateMetaData(data);

Delete

You can delete metadata stored to a user as below.

await sb.currentUser.deleteMetaData('key1');