node.js - 使用 Mongo/Express/Angular/Node 推送到模型中的数组

标签 node.js angularjs restangular mean-stack

我有一个名为 Patients 的模型和一个名为 Drugs 的模型。每个患者可以服用多种药物。

我正在使用 ng-repeat 循环遍历患者并显示。每个患者都会显示一个包含可用药物列表的选择框。我能够将所选药物的 ID 记录到控制台,但我似乎无法将其成功推送到模型。

< Node 模型>

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 模板>

<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>

期望的行为:当用户从选择框中选择药物时,所选药物的 ID 将添加到 patient.{patientID}.drugs,这是一个数组。

非常感谢您提供的任何帮助。

最佳答案

这个

$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);
};

可以缩短为

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

立即出现一个问题,您得到了 parentDrugs 数组,将 ID 插入其中,在您创建的名为 update 的对象中> 现在您再次将对象插入原始数组。

如果我理解正确,这应该可以。但我不确定

$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
};

关于node.js - 使用 Mongo/Express/Angular/Node 推送到模型中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21030920/

相关文章:

node.js - Mongoose - 使用单个集合的多个模式

javascript - 使用 TypeORM 使字段/列可选/必需

javascript - 如何使用 ng-repeat 索引动态生成的属性列表?

javascript - Restangular 请求未显示在(Chrome)网络选项卡中

java - SSE 与 angularJS 和 java in play 框架

node.js - npm 安装的生命周期

windows - EPERM,不允许操作错误,配置存储

javascript - 配置应用程序路由时如何注释内联 Controller 中的依赖项?

angularjs - 使用 Angular ui-grid 时的列标题换行

javascript - 选择具有矩形或 Angular 嵌套数组元素