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.