Announcement

Collapse
No announcement yet.

Getting id of a user in javascript

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting id of a user in javascript

    Hi,

    I am trying to add an iframe to the about page, but i am running into a problem.

    I am using javascript to get the user id of the current user, but it doesn't seem to work.

    I want to put the user ID at the end of the URL to show unique content for each user.

    The userID output is just blank for now, so can anyone see what i am doing wrong?

    The script:

    var re3 = "IFRAMEURL";
    var userID = this.getUser().id;

    document.getElementById('iframe').src = re3+userID;

    Thanks a lot.

  • #2
    Hi,

    The call to this.getUser().id is correct and should work as long as the class that contains your script is extended from a class that has that function defined, for example extended from a view

    to test, write console.log('userID = ',userId); under the line var userID = this.getUser().id; in your script and check your console to see if the script is actually pulling a user id or if it doesn't recognize the this.getUser().id call

    Comment


    • #3
      Hi again,

      It doesn't output anything in the console.log.

      And i can't see it at the end of the Url either.

      But don't you think it is due to the missing function to getUser in the about.js?

      Comment


      • #4
        Originally posted by hokmoc View Post
        But don't you think it is due to the missing function to getUser in the about.js?
        getUser() is defined in view.js and about.js is extended from view.js thus getUser() should be available to about.js

        Not sure why this.getUser().id doesn't work, but I tried these other ways and it does work:

        this.getUser().get("id")

        this._helper.user.attributes.id

        Code:
        define('views/about', 'view', function (Dep) {
        
            return Dep.extend({
        
                template: 'about',
        
                el: '#main',
        
                data: function () {
                    return {
                        version: this.getConfig().get('version')
                    };
               },
        
               setup: function () {
                    Dep.prototype.setup.call(this);
                    console.log('this.getUser().get("id") = ',this.getUser().get("id"));
                    console.log('this._helper.user.attributes.id = ',this._helper.user.attributes.id);
                }
        
            });
        });
        What I haven't tried, and don't even know if its possible, is how to modify about.js in a way that your changes will not be wiped out in the next upgrade.
        Last edited by telecastg; 05-29-2021, 04:18 AM.

        Comment


        • #5
          Thanks for the help.

          If i now want to be able to call the attributes like {{user}} how should i then do it. I have tried some diffrent approaches, but i can't get it to work:

          define('views/about', 'view', function (Dep) {

          return Dep.extend({

          template: 'about',

          el: '#main',

          data: function () {
          return {
          user: this.getUser().id
          };
          return {
          role: this.getUser().role
          };
          },

          data2: function () {
          return {
          role2: this.getUser().id
          };
          }

          });
          });

          Comment


          • #6
            The user attribute works, but i can't figure out how to add the other attributes.

            Comment


            • #7
              "user" is an entity, this.getUser() is a method of the "view" class that retrieves the current user entity/object.

              To retrieve entity/object attributes you call them like this:
              this.getUser().get("attribute name");
              For example this.getUser().get('id');

              To see what attributes are available for retrieval, write
              Code:
              user = this.getUser();
              console.log(user);
              then check your console to see what attributes are available.

              Comment

              Working...
              X