Как получить доступ к вложенному массиву объектов, используя mongoose? - PullRequest
0 голосов
/ 20 мая 2019

Итак, у меня есть массив объектов, который вложен в одну коллекцию. Моя главная проблема заключается в том, что когда я получаю доступ к этим вложенным объектам, он показывает только идентификаторы. Я хочу полные детали этих данных. Как мне это сделать?

Это модель proreg для регистрации продукта

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// Create Schema
const ProRegSchema = new Schema({

  refno1: {
    type: String
  },
  refno2: {
    type: String
  },
  date: {
    type: Date
  },
  customer: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customer" 
},
customertype: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customertype" 
},
department: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customersubdepartment" 
},

  products: [
    {
      oem: {
        type: Schema.Types.ObjectId,
        ref: 'product'
      },
      category: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "product"
      },
      subcategory: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "product"
      },
      modelno: {
        type: String
      },
      serialno: {
        type: String,
        required: true
      },
      warrantyfrom: {
        type: Date,
        default: "01-01-2019"
      },
      warrantyto: {
        type: Date,
        default: "01-01-2019"
      },
      oemwarrantyfrom: {
        type: Date,
        default: "01-01-2019"
      },
      oemwarrantyto: {
        type: Date,
        default: "01-01-2019"
      }, 
    }
  ],
  date: {
    type: Date,
    default: Date.now
  }
});

module.exports = ProReg = mongoose.model('proreg', ProRegSchema);

Это модель продукта, которая является эталонной в модели proreg

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

//Products Schema
const ProductSchema = new Schema({
  oem: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "company"
  },
  category: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "productcategory"
  },
  subcategory: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "productsubcategory"
  },
  modelno: { type: String },
  description: { type: String },
  specification: { type: String },
  productdoc: {type: String},
  date: {
    type: Date,
    default: Date.now
  }
});
module.exports = Product = mongoose.model("product", ProductSchema);

This is the get routes

    router.get('/:id', (req, res) => {
    	ProReg.findById(req.params.id)
    		.populate('customer', 'customername')
    		.populate('customertype', 'customertype')
    		.populate('department', 'department')
    		.populate('products.$.oem.$.companyname')
    		.then((proreg) => res.json(proreg))
    		.catch((err) => res.status(404).json({ msg: 'No Pro Reg found with that ID' }));
    });

Также это модели, вложенные в модель продукта

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

//Company Schema
const CompanySchema = new Schema({
  companyname: {
    type: String
  },
  contactperson: {
    type: String
  },
  contactno: {
    type: Number
  },
  alternatecontactno: {
    type: Number
  },
  email: {
    type: String
  },
  fax: {
    type: Number
  },
  address: {
    type: String
  },
  date: {
    type: Date,
    default: Date.now
  }
});
module.exports = Company = mongoose.model("company", CompanySchema);

//Product Category Schema
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const ProductCategorySchema = new Schema({
  category: {
    type: String
  },
  description: {
    type: String
  }
});
module.exports = ProductCategory = mongoose.model(
  "productcategory",
  ProductCategorySchema
);

//Product Sub Category Schema
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const ProductSubCategorySchema = new Schema({
  subcategory: {
    type: String
  },
  parentcategory: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "productcategory"
  },
  description: {
    type: String
  }
});
module.exports = ProductSubCategory = mongoose.model(
  "productsubcategory",
  ProductSubCategorySchema
);

И этот ответ, который я получаю proreg

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...