JavaScript
User Metadata

User Metadata

With a user metadata, you can store additional information to a channel. Use cases for the metadata are such as phone number, email or a long description of the user, which can each be fetched and rendered into the UI.


Create, update, retrieve, and delete a user metadata

A user metadata is a Dictionary that is stored into a User object. Its uses are flexible, allowing you to customize a user information to fit you and your users' needs.

Create

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

Light Color Skin
Copy
var data = {
    'key1': 'value1',
    'key2': 'value2'
};

var user = sb.currentUser;
user.createMetaData(data, function(metadata, error) {
    if (error) {
        return;
    }
});

Update

The process for updating a user metadata is identical to creation. Values are updated for existing keys, while new key-value pairs added.

Light Color Skin
Copy
var data = {
    'key1': 'valueToUpdate1',
    'key2': 'valueToUpdate2'
};

var user = sb.currentUser;
user.updateMetaData(data, function(metadata, error) {
    if (error) {
        return;
    }
});

Retrieve

A user metadata is one of the properties of a User object. Using the dot and bracket notations, you can get a stored item of a user metadata with its unique key.

Light Color Skin
Copy
var user = sb.currentUser;
var value1 = user.metaData['key1'];

Delete

The process for deleting a user metadata is as simple as creating one.

Light Color Skin
Copy
var user = sb.currentUser;
user.deleteMetaData('key1', function(response, error) {
    if (error) {
        return;
    }
});