Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a model called Patients and a model called Drugs. Each patient can be taking multiple drugs.

I'm using ng-repeat to loop through the patients and display. Each patient displays a select box containing the list of available drugs. I am able to log the id of the selected drug to the console, but I can't seem to successfully push it to the model.

<Node Models >

Patient.model.js

    module.exports = function (mongoose, name) {
        var mongoose = require('mongoose'),
        Schema = mongoose.Schema,
        ObjectId = Schema.ObjectId;

        var schema = mongoose.Schema({
            name: String,
            drugs: [String],
            tests: [String]
        });

        mongoose.model(name, schema);
    };

Drug.model.js

    module.exports = function (mongoose, name) {
        var mongoose = require('mongoose'),
        Schema = mongoose.Schema,
        ObjectId = Schema.ObjectId;

        var schema = mongoose.Schema({
            name: String
        });

        mongoose.model(name, schema);
    };

< Node Controller >

Patient.controller.js

var mongoose = require('mongoose'),
    Patient = mongoose.model('Patient');

exports.create = function (req, res) {
  var patient = new Patient(req.body);
  patient.save(function (err) {
    if (err) {
      res.send(400, err);
    } else {
      res.send(patient);
    }
  });
};

exports.read = function (req, res) {
  Patient.findById(req.params.id, function (err, patient) {
    if (err) {
      res.send(400, err);
    } else if (!patient) {
      res.send(404);
    } else {
      res.send(patient);
    }
  });
};

exports.update = function (req, res) {
  var id = req.params.id,
      data = req.body;

  delete data._id; // Just in case...

  Patient.findByIdAndUpdate(id, data, function (err) {
    if (err) {
      res.send(400, err);
    } else {
      res.send({success: true, msg: 'saved'});
    }
  });
};

exports.del = function (req, res) {
  var id = req.params.id;

  Patient.findById(id, function (err, patient) {
    if (err) {
      res.send(400, err);
    } else if (!patient) {
      res.send(404);
    } else {
      patient.remove(function (err) {
        if (err) {
          res.send(400, err);
        } else {
          res.send({success: true, msg: 'removed'});
        }
      });
    }
  });
};

exports.search = function (req, res) {
  Patient.find(req.query, function (err, patients) {
    if (err) {
      res.send(400, err);
    } else {
      res.send(patients);
    }
  });
};

< Angular Controller >

patientController.js

  angular
    .module('dpMean.patient')
    .controller('PatientCtrl', function ($scope, Restangular, $log) {
      'use strict';
      var Patient = Restangular.all('patient');
      Patient.getList()
        .then(function (patients) {
          $scope.patients = patients;
        })
        .catch(function (err) {
          $log.error(err);
        });
      var Drug = Restangular.all('drug');
      Drug.getList()
        .then(function (drugs) {
          $scope.drugs = drugs;
        })
        .catch(function (err) {
          $log.error(err);
        });

        /* $scope.add and $scope.delete live here */

      $scope.select = function(id, pid){
        console.log(id);

        var patientDrugs = Restangular.one("patient", pid);
        var array = patientDrugs;
        console.log(array);
        var newarray = array.push(id);
        var update = {
          drugs: newarray
        }
        console.log(patientDrugs);
        patientDrugs.put(update);
      };
    });

< Angular Template >

<div>
    <h3>Patients</h3>
    <ul class="unstyled">
      <li class="patient-item" ng-repeat="patient in patients">
        <h4>{{patient.name}}</h4>
        <label>Add Drug</label>
        <select ng-options="drug.name for drug in drugs" ng-model="drug" ng-change="select(drug._id, patient._id)">
        </select>
        <ul>
            <li ng-repeat="drug in patient.drugs">
                {{drug}}
            </li>
        </ul>
        {{ patient.drugs}}

        <hr />
        <a ng-click="delete(patient._id)">[Remove Patient]</a>

      </li>
      <li class="patient-item">
        <form ng-submit="add()">
          <input type="text" placeholder="New item..." ng-model="name"/>
          <button type="submit" ng-disabled="posting || !name">Add</button>
        </form>
      </li>
    </ul>
</div>

Desired behavior: When user selects a drug from the select box, the id of the selected drug will be added to patient.{patientID}.drugs, which is an array.

Thanks so much for any help you can give.

share|improve this question
    
Where is the patient array in the patientController.js? –  dcodesmith Jan 9 at 21:21
add comment

1 Answer

This

$scope.select = function(id, pid){
  console.log(id);

  var patientDrugs = Restangular.one("patient", pid);
  var array = patientDrugs;
  console.log(array);
  var newarray = array.push(id);
  var update = {
    drugs: newarray
  }
  console.log(patientDrugs);
  patientDrugs.put(update);
};

can be shortened to

$scope.select = function(id, pid){
  var patientDrugs = Restangular.one("patient", pid);
  var update = {
    drugs: patientDrugs.push(id)
  }
  patientDrugs.put(update);
};

and immediately a problem pops up, You get the parientDrugs array, you push the ID into it, within an object you create called update and now you push the object into the original array again.

If I understand correctly, this should work. But I am not sure

$scope.select = function(id, pid){
  var patientDrugs = Restangular.one("patient", pid);
  patientDrugs.push(({drugs: id})); //This line depends on what the patientDrugs object looks like
};
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.