簡體   English   中英

使用Mongo / Express / Angular / Node推送到模型中的數組

[英]Push to array within a model using Mongo/Express/Angular/Node

我有一個名為Patients的模型和一個名為Drugs的模型。 每位患者都可以服用多種葯物。

我正在使用ng-repeat來遍歷患者並展示。 每位患者都會顯示一個包含可用葯物列表的選擇框。 我能夠將所選葯物的ID記錄到控制台,但我似乎無法成功將其推送到模型。

< 節點模型 >

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

<節點控制器>

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

<角度控制器>

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>

期望的行為:當用戶從選擇框中選擇葯物時,所選葯物的ID將被添加到患者。{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);
};

彈出一個問題,你得到了parientDrugs數組,你將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
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM