Custom Fields

You can add some custom fields (e.g. "age", "gender" and "birthday") too. You can use any field names as long as they are not predefined by the SDK. To learn more about the predefined fields, please read the JSDoc.

Setting and updating custom fields

The following example illustrates how to set the custom fields. You can delete custom fields by passing the field name in the array.

  • // Get the currently logged-in user.
    var user = KiiUser.getCurrentUser();
    
    // Set the user identify.
    var identityData = {
      "username": "user_123456",
      "emailAddress": "user_123456@example.com",
      "phoneNumber": "+819012345678"
    };
    
    // Set custom fields.
    var customFields = {
      "age": 30,
      "score": 12344300
    };
    
    // Remove a custom filed.
    var removedFields = ["weight", "chest"];
    
    // Update the user attributes.
    user.update(identityData, null, customFields, removedFields).then(
      function(theUser) {
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
        var theUser = error.target;
        var errorString = error.message;
      }
    );
  • // Get the currently logged-in user.
    var user = KiiUser.getCurrentUser();
    
    // Set the user identify.
    var identityData = {
      "username": "user_123456",
      "emailAddress": "user_123456@example.com",
      "phoneNumber": "+819012345678"
    };
    
    // Set custom fields.
    var customFields = {
      "age": 30,
      "score": 12344300
    };
    
    // Remove a custom filed.
    var removedFields = ["weight", "chest"];
    
    // Update the user attributes.
    user.update(
      identityData,
      {
        success: function(theUser) {
          // Do something.
        },
        failure: function(theUser, errorString) {
          // Handle the error.
        }
      },
      customFields,
      removedFields
    );

If an existing field is not explicitly specified for the update operation, the field remains unchanged on Kii Cloud.

The fields are updated on Kii Cloud and in the KiiUser instance after the update() method is successfully completed.

You can update predefiend and custom fields at the same time.

Only users themselves can update their fields. See User Attributes to learn more on who can access the user's fields.

Getting custom fields

The following example illustrates how to get the custom fields.

  • // Get the currently logged-in user.
    var user = KiiUser.getCurrentUser();
    
    // Refresh the user to get the latest user info from Kii Cloud.
    user.refresh().then(
      function(theUser) {
        // Get custom fields.
        var age = theUser.get("age");
        var gender = theUser.get("male");
        var height = theUser.get("height");
        var isMember = theUser.get("isMember");
      }
    ).catch(
      function(error) {
        // Handle the error.
        var theUser = error.target;
        var errorString = error.message;
      }
    );
  • // Get the currently logged-in user.
    var user = KiiUser.getCurrentUser();
    
    // Refresh the user to get the latest user info from Kii Cloud.
    user.refresh({
      success: function(theUser) {
        // Get custom fields.
        var age = theUser.get("age");
        var gender = theUser.get("male");
        var height = theUser.get("height");
        var isMember = theUser.get("isMember");
      },
      failure: function(user, errorString) {
        // Handle the error.
      }
    });

Please call the get method to get the field values.

Setting custom fields upon the user registration

The following example illustrates how to set the custom fields upon the user registration.

As shown in the sample code, you can set the custom fields with the set method after creating a KiiUser instance.

  • // Create a user.
    var username = "user_123456";
    var password = "123ABC";
    var user = KiiUser.userWithUsername(username, password);
    
    // Set custom fields.
    user.set("age", 30);
    user.set("score", 0);
    
    // Register the user.
    user.register().then(
      function(theUser) {
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
        var theUser = error.target;
        var errorString = error.message;
      }
    );
  • // Create a user.
    var username = "user_123456";
    var password = "123ABC";
    var user = KiiUser.userWithUsername(username, password);
    
    // Set custom fields.
    user.set("age", 30);
    user.set("score", 0);
    
    // Register the user.
    user.register({
      success: function(theUser) {
        // Do something.
      },
      failure: function(theUser, errorString) {
        // Handle the error.
      }
    });