1

Project Link: https://github.com/k4u5hik/node-express-course

I'm attempting to use populate.js to auto import data from products.json into my MongoDB database using mongoose. Schema is in product.js.

product.js

const mongoose = require('mongoose')

const productSchema = new mongoose.Schema({
  name: {
    type: String,
    required: [true, 'product price must be provided'],
  },
  price: {
    type: Number,
    required: [true, 'product price must be provided'],
  },
  featured: {
    type: Boolean,
    default: false,
  },
  rating: {
    type: Number,
    default: 4.5,
  },
  createdAt: {
    type: Date,
    default: Date.now(),
  },
  company: {
    type: String,
    enum: {
      values: ['ikea', 'liddy', 'caressa', 'marcos'],
      message: '{VALUE} is not supported',
    },
    // enum: ['ikea', 'liddy', 'caressa', 'marcos'],
  },
})

module.exports = mongoose.model('Product', productSchema)

populate.js

require ('dotenv').config()

const connectDB = require('./db/connect')
const Product = require('./models/product')

const jsonProducts = require('./products.json')

const start = async () => {
    try {
        await connectDB(process.env.MONGO_URI)
        await Product.deleteMany()
        await Product.create(jsonProducts)
        console.log('Products created or updated on database')
        process.exit(0)
    } catch (error) {
        console.log(error);
    }
}

start()

Terminal Error

kaushik@Madhuras-Air 04 - Practice % node populate
/Users/kaushik/WebstormProjects/node-express-course/04 - Practice/node_modules/mongoose/lib/schema.js:1044
    throw new TypeError(`Invalid schema configuration: \`${name}\` is not ` +
    ^

TypeError: Invalid schema configuration: `String` is not a valid type at path `name`. See bit.ly/mongoose-schematypes for a list of valid schema types.
    at Schema.interpretAsType (/Users/kaushik/WebstormProjects/node-express-course/04 - Practice/node_modules/mongoose/lib/schema.js:1044:11)
    at Schema.path (/Users/kaushik/WebstormProjects/node-express-course/04 - Practice/node_modules/mongoose/lib/schema.js:684:27)
    at Schema.add (/Users/kaushik/WebstormProjects/node-express-course/04 - Practice/node_modules/mongoose/lib/schema.js:536:14)
    at new Schema (/Users/kaushik/WebstormProjects/node-express-course/04 - Practice/node_modules/mongoose/lib/schema.js:132:10)
    at Object.<anonymous> (/Users/kaushik/WebstormProjects/node-express-course/04 - Practice/models/product.js:3:23)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/kaushik/WebstormProjects/node-express-course/04 - Practice/populate.js:4:17)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)

I have asked my tutor and he was able to run my code locally on his computer without any issue. The problem he said is not in the code, I double checked the ENV var and it seems correct to me as well. I am unable to get any real solution to this and the learning has come to an abrupt halt for last 3 days.

Not sure what I can do to resolve this. Would appreciate any suggestions from the community.

Kaushik C
  • 11
  • 6
  • I can run it on my computer too. What's your node version? – Benny Yen Nov 26 '21 at 02:16
  • My node version was v16.13.0. I figured out what the issue was. I had the latest mongoose version. I downgraded it to version 5.11.10 which was what the tutor had installed and it worked. – Kaushik C Nov 26 '21 at 02:43
  • I try to use the latest mongoose version and when I removed connect options in `connect.js` it worked. https://stackoverflow.com/questions/68958221/mongoparseerror-options-usecreateindex-usefindandmodify-are-not-supported – Benny Yen Nov 26 '21 at 02:57

1 Answers1

0

I downgraded my mongoose to version 5.11.10 which was what the tutor had installed and it worked. Figured that the latest version was causing this error.

EDIT: Benny has resolved the issue.

Kaushik C
  • 11
  • 6