Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
71 views
in Technique[技术] by (71.8m points)

javascript - Unable to get the values from one schema to another schema

I create a order schema which have two models (StudentConsessionSchema,consessionSchema) . StudentConsessionSchema acquire the student model (student model contain the details about student). Another model consessionSchema acquire the StudentConsessionSchema (for acquiring the student details) . When I test it to order with postman it works but not able to get student details in consessionSchema .

order schema

const mongoose = require("mongoose");
const { ObjectId } = mongoose.Schema;

const StudentConsessionSchema = new mongoose.Schema({
  student: {
    type: ObjectId,
    ref: "Student", //ref to the student model from another file
  },
  FullName: {
    type: String,
  },
  addmissionNumber: {
    type: String,
    required: true,
    trim: true,
    maxlength: 8,
  },
  faculty: {
    type: ObjectId, //ref to the faculty model from another file
    ref: "Faculty",
    required: true,
  },
});

const StudentConsession = mongoose.model(
  "StudentConsession",
  StudentConsessionSchema
);

const consessionSchema = new mongoose.Schema(
  {
    student: [StudentConsessionSchema], //this returns [] empty array
     consession_id: {},

    fromStation: {
      type: String,
      required: true,
    },
    toStation: {
      type: String,
      default: "Dadar",
    },
    passType: {
      type: String,
      enum: ["I", "II"],
      required: true,
    },
    ticketPeriod: {
      type: String,
      enum: ["Monthly", "Quarterly"],
      default: "Monthly",
      required: true,
    },
    status: {
      type: String,
      default: "Pending",
      enum: ["Pending", "Cancelled", "Accepted", "Printed"],
    },
    updated: Date,
  },
  { timestamps: true }
);

const Consession = mongoose.model("Consession", consessionSchema);

module.exports = { StudentConsession, Consession };

create order controller

exports.createOrder = (req, res) => {
  let form = new formidable.IncomingForm();
  form.keepExtensions = true;

  form.parse(req, (err, fields) => {
    const {
      admissionNumber,
      fullName,
      faculty,
      fromStation,
      toStation,
      passType,
      ticketPeriod,
    } = fields;

    if (
      !admissionNumber ||
      !fullName ||
      !faculty ||
      !fromStation ||
      !toStation ||
      !passType ||
      !ticketPeriod
    ) {
      return res.status(400).json({
        error: "Please fill all fields",
      });
    }
    let consession = new Consession(fields);
    // const consession = new Consession(req.body.consession);
    if (req.student.pass === 1) {
      return res.status(400).json({
        error:
          "You are not eligible to make request Because You already have a pass",
      });
    }

    consession.save((err, order) => {
      if (err) {
        console.log(err);
        return res.status(400).json({
          error: "Failed to make order",
        });
      }
      res.json(order);
    });
  });
};

output

{
    "toStation": "abc",
    "ticketPeriod": "Monthly",
    "status": "Pending",
    "_id": "600e789887b9a201bc4c2d1a",
    "fromStation": "xyz",
    "passType": "II",
    "student": [],
    "createdAt": "2021-01-25T07:51:52.091Z",
    "updatedAt": "2021-01-25T07:51:52.091Z",
    "__v": 0
}

why student array is empty?

question from:https://stackoverflow.com/questions/65880841/unable-to-get-the-values-from-one-schema-to-another-schema

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...