title: Accessing an associated table from a Sequelize getter method
body: <p>This is my first node.js web app after 20 years of C++, and I think I just need someone to point me in the right direction here.</p>
  
  <p>I'm writing the server side of an app that deals with schools and students. When the client sends a request (via my REST API) for a particular student, I want to return the student's first and last name and school. I'm using a library called epilogue that parses the requests, reads/writes to the MySQL database via sequelize, and wraps up the result in a JSON response. It will send all the fields I have defined for student as well as the values of any getters I have defined. Everything has been working great. I have been able to use epilogue to POST, PUT, GET and generally keep the MySQL database updated. </p>
  
  <p>However, things got tricky when I tried to define a getter method that returns the student's school name. In order to do that, I have to access a record in the school table and get the school name. </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var school = sequelize.define("School", {
      SchoolName: {
        type: DataTypes.STRING,
      },
    }, {
      classMethods: {
        associate: function(models) {
          school.hasMany(models.Student);
        }
      }
    }
  }
  
  var student = sequelize.define("Student", {
      FirstName: {
        type: DataTypes.STRING,
      },
      LastName: {
        type: DataTypes.STRING,
      },
    }, {
      classMethods: {
        associate: function(models) {
          student.belongsTo(models.School);
        },
      },
      getterMethods: {
        FullName: function() {
          return this.FirstName + ' ' + this.LastName; // This works just fine
        },
        SchoolName: function() {
          return this.getSchool().SchoolName; // This doesn't work because getSchool is asynchronous
        }
      }
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>The problem is that .getSchool() is asynchronous, so it seems I would have to use .then to access the result. Promises are a new concept for me, but I've tried things like:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>SchoolName: function {
    return this.getSchool.then(function(school) {
      return school.SchoolName;
    });
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>But it seems this just returns the Promise itself instead of the results of the promise because the promise isn't fulfilled at the time the getter returns. So, I end up sending the client something like:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>{
    "FullName": "John Doe", 
    "SchoolName": {
      "isFulfilled": false, 
      "isRejected": false
    },
    "id":1,
    "FirstName":"John",
    "LastName":"Doe"
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>It seems like the getters here are necessarily synchronous, so it looks like I just need some way to tell the getter to stop and wait until the Promise is fulfilled and then return the value. But that seems to violate the asynchronous nature of node.js. So, I have a hunch that I am fundamentally misunderstanding something important about sequelize.js or node.js or promises or maybe even database design. Can someone redirect me, please? Mainly, I would like to know: what is the <em>right</em> way to do this?</p>