Trying to seed to mongoDB with an array of string, not sure what I am supposed to declare the model as

58 Views Asked by At

So I am trying to test some seed data, with mongoDB, it is an ecommerce store and I am trying to parse in an array of categories but it gives an error. This is the products model

const { Schema, model } = require('mongoose');
const dateFormat = require('../utils/dateFormat');

const productSchema = new Schema({
  title: {
    type: String,
    required: 'The Product title is required',
    minlength: 1,
    maxlength: 280,
    trim: true,
  },
  description: {
    type: String,
    required: true,
    trim: true,
  },
  categories: [{
    type: Schema.Types.ObjectId,
    ref: 'Category',
  }],
  brand: {
    type: Schema.Types.ObjectId,
    ref: 'Brand',
    required: false
},
  image: {
    type: String,
    required: true,
    trim: true,
  },
  price: {
    type: Number,
    required: true,
    trim: true,
  },
  createdAt: {
    type: Date,
    default: Date.now,
    get: (timestamp) => dateFormat(timestamp),
  }
});

const Product = model('Product', productSchema);

module.exports = Product;

Brand model:

const { Schema, model } = require('mongoose');
const dateFormat = require('../utils/dateFormat');

const brandSchema = new Schema({
  name: {
    type: String,
    required: 'The Brand name is required',
    minlength: 1,
    maxlength: 280,
    trim: true,
  },
  products: [{
    type: Schema.Types.ObjectId,
    ref: 'Product',
  }],
  createdAt: {
    type: Date,
    default: Date.now,
    get: (timestamp) => dateFormat(timestamp),
  }
});

const Brand = model('Brand', brandSchema);

module.exports = Brand;

Category model

const { Schema, model } = require('mongoose');
const dateFormat = require('../utils/dateFormat');

const categorySchema = new Schema({
  name: {
    type: String,
    required: 'The Category name is required',
    minlength: 1,
    maxlength: 280,
    trim: true,
  },
  products: [{
    type: Schema.Types.ObjectId,
    ref: 'Product',
  }],
  createdAt: {
    type: Date,
    default: Date.now,
    get: (timestamp) => dateFormat(timestamp),
  }
});

const Category = model('Category', categorySchema);

module.exports = Category;

and this is a snippet of the seed data

    {
      "title": "Ansie Boots",
      "description": "2",
      "image": "http://via.placeholder.com/640x360",
      "price": 145,
      "categories": ["Boots", "For Her"],
      "brand":"Vagabond"
    }

I was reading the MongoDB docs and came across this $facet doc but I was a bit puzzled about what it was trying to do, as I am still a beginner.

 errors: {
    'categories.0': CastError: Cast to [ObjectId] failed for value "[ 'Trainers', 'For Him' ]" (type string) at path "categories.0" because of "CastError"

The error codes show me that the brands also

  stringValue: '"Nike"',
      messageFormat: undefined,
      kind: 'ObjectId',
      value: 'Nike',
      path: 'brand',
      reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
1

There are 1 best solutions below

0
Joe On

In the Product model brand and categories are defined as ObjectIds that reference other models.

The the seed data these fields contain strings.

You will need to create instances of the Brand and Category models for each of those before you can refer to them.

If the seed process will be single-threaded, you might use something like this to get a brand document:

let branddoc = Brand.findOne({name: seedItem.name});
if (branddoc == null) { 
   branddoc = new Brand({name:seedItem.name})
   branddoc.save()
}

Then replace the brand string in the seed item with the generated document, do the same process with each entry in the array of categories before creating the product document.