title: extract content from Object, Nodejs
body: <p>I'm using the MEAN stack and are trying to extract info from an object, but the attributs change depending on lots of factors so can't call them. Is there a way I can get the information in another way? The data will be used to update different things in my monogdb. </p>
  
  <p>Simple info about my database: 3 collections. Users, Years, Courses.</p>
  
  <p>Year: id, userid, courses {courseid, grade, }</p>
  
  <p>Course: id, name, credits etc. </p>
  
  <p>I've managed to send the information to the nodejs file but can't do anything with it. Below is the result of the consolelog I do of the information. How do I get the information out of the object so that I can update my database? </p>
  
  <p><strong>Result from the log</strong></p>
  
  <pre><code>Object {year: Object, grade: Object}
      grade:Object
            574d8ce6bfb4ea470a2e10c7:"3"
            __proto__:Object
      year:Object
           574e8cf5ae8fc00917ded99b:true
           __proto__:Object
  
    Grade contains the id of the course for which I want to change the grade and the grade 
    I want to change to. 
    Year contains the Id of the year in which the course is stored. 
  </code></pre>
  
  <p><strong>home/index.html</strong></p>
  
  <p>Below only contains the form. The variable year.XXX iterates over a year (Retrieved from the database). </p>
  
  <p>year.course = {id: XX, grade:YY}</p>
  
  <p>theCourse iterates over all courses in the database to get course-specific information. </p>
  
  <p>vm.change is the variable sent on to the controller. </p>
  
  <pre><code> &lt;form&gt;
       &lt;div class="panel-heading " &gt;&lt;b&gt;  YEAR {{year.year}}&lt;/b&gt;
           &lt;label style="float: right;"&gt; Edit
               &lt;input type="checkbox" id="year._id" value="year.id" ng-model="vm.change.year[year._id]"/&gt;
                  &lt;/label&gt;
                  &lt;div&gt;&lt;p&gt;Sum of Credits: {{year.totalCredits}}&lt;/p&gt;&lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="panel-body hideyear" &gt;
                  &lt;div class="col-xs-12"&gt;
                      &lt;h4&gt;HT&lt;/h4&gt;
                      &lt;table class="table " &gt;
                          &lt;tr class="active" &gt;
                              &lt;th&gt;&lt;b&gt;Course code&lt;/b&gt;&lt;/th&gt;
                              &lt;th&gt;&lt;b&gt;Grade&lt;/b&gt;&lt;/th&gt;
                              &lt;th&gt;&lt;b&gt;Update Grade&lt;/b&gt;&lt;/th&gt;
                          &lt;/tr&gt;
                          &lt;tbody ng-repeat= "course in year.courses"&gt;
                          &lt;tr ng-repeat="theCourse in vm.courses" ng-if="theCourse._id==course.id &amp;&amp; (theCourse.term=='HT1'||theCourse.term=='HT2')"&gt;
                              &lt;td &gt;{{theCourse.courseCode}}&lt;/td&gt;
                              &lt;td&gt;{{course.grade}}&lt;/td&gt;
                              &lt;td &gt;&lt;select ng-disabled="!vm.change.year[year._id]" ng-model="vm.change.grade[course.id]"&gt;
                                  &lt;option disabled selected value&gt; -- update grade -- &lt;/option&gt;
                                  &lt;option value="NA"&gt;NA&lt;/option&gt;
                                  &lt;option value="5"&gt;5&lt;/option&gt;
                                  &lt;option value="4"&gt;4&lt;/option&gt;
                                  &lt;option value="3"&gt;3&lt;/option&gt;
                                  &lt;option value="G"&gt;G&lt;/option&gt;
                                  &lt;option value="U"&gt;U&lt;/option&gt;
                              &lt;/select&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;/tbody&gt;
                      &lt;/table&gt;
                  &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;button type="submit"  class="btn btn-primary" ng-disabled="!vm.change.year[year._id]" ng-click="vm.saveChange(vm.change)"&gt;Save&lt;/button&gt;
              &lt;/div&gt;
              &lt;/form&gt;
  </code></pre>
  
  <p><strong>index.controller.js</strong></p>
  
  <pre><code> function saveChange(change){
          console.log(change)
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>In a file called <strong>app.js</strong> vm to Controller as and choose which controller to use: </p>
  
  <pre><code>(function () {
      'use strict';
  
      angular
          .module('app', ['ui.router'])
          .config(config)
          .run(run);
  
      /*
      Connects the right subpage with corresponding controller and url.
      */
  
      function config($stateProvider, $urlRouterProvider) {
          // default route
          $urlRouterProvider.otherwise("/");
  
          $stateProvider
          .state('home', {
              url: '/',
              templateUrl: 'home/index.html',
              controller: 'Home.IndexController',
              controllerAs: 'vm',
              data: { activeTab: 'home' }
          });
      }
  </code></pre>
  
  <p>When the html loads, the controller gathers some information from the database thats used on most parts of the page. </p>
  
  <pre><code>function initController() {
          // get current user
          UserService.GetCurrent().then(function (user) {
              vm.user = user;
  
          });
          //Gets all years in the database
          YearService.GetAll().then(function (years) {
              vm.years = years;
          });
          // Gets all courses in the databased.
          CourseService.GetAll().then(function (courses) {
              vm.courses = courses;
          });
      }
  </code></pre>
  
  <p>All the gets in the initcontroller above does pretty much the same thing, so I'll show the process for one of them, YearSevice.GetAll, calls <strong>app-service/year.service.js</strong></p>
  
  <pre><code>(function () {
  'use strict';
  
      angular
          .module('app')
          .factory('YearService', Service);
  
      function Service($http, $q){
          var service =  {};
          service.GetAll = GetAll;
          return service;
  
          function GetAll() {
          return $http.get('/api/year/getAll').then(handleSuccess, handleError);
          }
  </code></pre>
  
  <p>with the $http.get we go via the server to a server-side controller, <strong>year.controller</strong>.</p>
  
  <pre><code>var config = require('config.json');
  var express = require('express');
  var router = express.Router();
  var YearService = require('services/year.service');
  
  // routes
  
  router.get('/getAll', getAll);
  
  function getAll(req,res){
      YearService.getAll()
           .then(function(result){
              if(result){
                  res.send(result);
              }else{
                  res.sendStatus(404);
              }
          })
           .catch(function(err){
              res.status(400).send(err);
           });
  }
  </code></pre>
  
  <p>YearService.getAll calls the server-side services: services/year.services.js</p>
  
  <pre><code>var config = require('config.json');
  var _ = require('lodash');
  var Q = require('q');
  var mongo = require('mongoskin');
  
  
  //Connectionsstring to the mongo database, if the app is runed on openshift the connectionstring value will change.
  var connectionString = "mongodb://localhost:27017/mean-stack-registration-login-example";
  if(process.env.OPENSHIFT_MONGODB_DB_URL){
      connectionString = process.env.OPENSHIFT_MONGODB_DB_URL + "studycontrol";
  }
  var db = mongo.db(connectionString, { native_parser: true });
  
  
  db.bind('years');
  db.bind('users');
  db.bind('courses');
  
  var service = {};
  service.getAll = getAll;
  module.exports = service;
  
  
  function getAll(){
      var deferred = Q.defer();
  
      //
      db.collection('years').find().toArray(function(err, result) {
          if (err) deferred.reject(err);
          deferred.resolve(result);
          return deferred.promise;
      });
      return deferred.promise;
  
  }
  </code></pre>
  
  <p>And the collection is returned allt the way to the html-code. Sending new information to the database has the same structure but includes an object to be sent with the whole time. In previous cases the object have been easy to construct, but now I need to send course-code, year-id and grade and am trying to do it using a checkbox and a select, so three values but in two "carriers". </p>