объединение данных для 3 моделей с использованием mon goose, node js, express js - PullRequest
0 голосов
/ 11 февраля 2020

Я хочу связать 3 модели "перевод * ca * 1022", "реквизиты субсчета", "банковские депозиты". То, что я хочу сделать, это то, что эти три имеют общий получить требуемый "Current_balance" «Current_balance» должен быть обновлен в 3 моделях, связав их. Это моя схема и контроллеры

BankdepositModel. js

var mongoose = require("mongoose");

var BankDepositsSchema = new mongoose.Schema(
  {
    source_sub_account: Array,
    amount: Number,
    cheque_notes: Number,
    to_bank_account: Array,
    amount_in_words: String,
    bank_Ref_no: String
  },
  {
    timestamps: true
  }
);

module.exports = mongoose.model("BankDeposits", BankDepositsSchema);

Subaccountdetailsmodel. js

var mongoose = require("mongoose");

var SubAccountDetailsSchema = new mongoose.Schema(
  {
    sub_account_name: String,
    current_balance: Number,
    account: Array
  },
  {
    timestamps: true
  }
);

module.exports = mongoose.model("SubAccountDetails", SubAccountDetailsSchema);

cashtransfermodel. js

var mongoose = require('mongoose');
var CashTransferSchema = new mongoose.Schema 
    ({
        source_sub_account:Array,
        description:String,
        recieved_by:String,
        amount:Number,
        date_time_transferred:Date,
        destination_sub_account:Array,
        amount_in_words:String,
            },
    {
        timestamps:true
    });
module.exports =mongoose.model('CashTransfer', CashTransferSchema);

Контроллер. js

var BankDeposits = require("../model/bankdepositmodel");
var SubAccountDetails = require("../model/subaccountsmodel.js");

exports.create1 = (req, res) => {
  var BankDeposit = new BankDeposits({
    source_sub_account: req.body.source_sub_account,
    amount: req.body.amount,
    cheque_notes: req.body.cheque_notes,
    to_bank_account: req.body.to_bank_account,
    amount_in_words: req.body.amount_in_words,
    bank_ref_no: req.body.bank_ref_no
  });

  BankDeposit.save()
    .then(data1 => {
      res.send(data1);
    })
    .catch(err => {
      res.status(500).send({
        message: err.message
      });
    });
};

//BankDeposit get
exports.find1 = (req, res) => {
  BankDeposits.aggregate([
    {
      $lookup: {
        from: "SubAccountDetails",
        localField: "current_balance",
        foreignField: "current_balance",
        as: "balance"
      }
    }
  ])
    .then(appdata => {
      res.status(200).send(appdata); //On successful fetch, server responds with status 200
    })
    .catch(err => {
      res.status(400).send(err); //On error, server responds with status 400
    });
};
//sub account details post
exports.createSubAccountDetail = (req, res) => {
  var SubAccountDetail = new SubAccountDetails({
    sub_account_name: req.body.sub_account_name,
    current_balance: req.body.current_balance,
    account: req.body.account
  });

  SubAccountDetail.save()
    .then(SubAccountDetail => {
      res.send(SubAccountDetail);
    })
    .catch(err => {
      res.status(500).send({
        message: err.message
      });
    });
};

//sub account details get
exports.SubAccountDetail = (req, res) => {
  SubAccountDetails.find()
    .then(SubAccountDetails => {
      res.send(SubAccountDetails);
    })
    .catch(err => {
      res.status(500).send({
        message: err.message || "Some error occurred while retrieving regs."
      });
    });
};

//CashTransfers Post Request
exports.create5 = (req, res) => {

 var Cash = new CashTransfer 
    ({
        source_sub_account:req.body.source_sub_account,
        description:req.body.description,
        recieved_by:req.body.recieved_by,
        amount:req.body.amount,
        date_time_transferred:req.body.date_time_transferred,
        destination_sub_account:req.body.destination_sub_account,
        amount_in_words:req.body.amount_in_words,
    });

    Cash.save()
    .then(data5 => {
        res.send(data5);
    }).catch(err => {
        res.status(500).send({
            message: err.message
        });
     });    
};

//Cash Transfers Get Request
exports.find5 = (req, res) => {
 CashTransfer.find()
    .then(CashTransfers => {
        res.send(CashTransfers);
    }).catch(err => {
        res.status(500).send({
            message: err.message || "Some error occurred while retrieving regs."
        });
    });
};
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...