メニュー

Expand
ページを評価:

同期マップ

A map stores unordered JSON objects accessible via a developer defined key.

Maps can be created, updated, subscribed to and removed via the client JavaScript SDK. Servers wishing to manage these objects can do so via the REST API.

Adding JSON to or Updating existing JSON in a map

syncClient.map('users').then(function(map) {
  map.set('david', {
    phone_number: 12345678,
    country: 'UK'
  }).then(function(item) {
    console.log('Added: ', item.key);
  }).catch(function(err) {
    console.error(err);
  });
});

Using set will overwrite any existing data in a map item.

Updating data in a map item

syncClient.map('users').then(function(map) {
  map.update('david',{country: "IRL"});
});

Use mutate for more control over updates.

syncClient.map('users').then(function (map) {
  map.mutate('david',function(remoteData) {
    remoteData.country = "USA";
    return remoteData;
  });
});

For more details on mutate and conflict resolution read up on Mutation and Conflict Resolution.

Subscribing to a map

syncClient.map('users').then(function (map) {
  map.on('itemAdded', function(item) {
    console.log('key', item.key);
    console.log('JSON data', item.value);
  });

//Note that there are two separate events for map item adds and map item updates:
  map.on('itemUpdated', function(item) {
    console.log('key', item.key);
    console.log('JSON data', item.value);
  });
});

Get items from a map

syncClient.map('users').then(function(map) {
  map.getItems().then(function(page) {
    console.log('show first item', page.items[0].key,
      page.items[0].value);
  });
});

Delete a map

syncClient.map('users').then(function (map) {
  map.removeMap().then(function () {
    console.log('map deleted');
  });
});

Get a particular item from a map

syncClient.map('users').then(function(map) {
  map.get('david').then(function(item) {
    console.log(item.value);
  });
});

Delete a particular item from a map

syncClient.map('users').then(function(map) {
  map.remove('david').then(function() {
    console.log('item deleted');
  });
});

See the latest JavaScript SDK documentation for a full reference.

ページを評価:

ヘルプが必要ですか?

誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。

        
        
        

        フィードバックくださりありがとうございます!

        We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

        Sending your feedback...
        🎉 Thank you for your feedback!
        Something went wrong. Please try again.

        Thanks for your feedback!

        Refer us and get $10 in 3 simple steps!

        ステップ1

        Get link

        Get a free personal referral link here

        ステップ2:

        Give $10

        Your user signs up and upgrade using link

        ステップ3

        Get $10

        1,250 free SMSes
        OR 1,000 free voice mins
        OR 12,000 chats
        OR more