skip to Main Content

i have a problem with adding a collection into my database in mongodb atlas.
I have managed to import this collection before but i accidentally deleted it and now i can’t upload it again. there is no error in my terminal. There for i don’t know what is wrong with my code.. (image of my code and terminal are attached below)

There is anyone who might know why is this can happen?

EDIT

I tried to open a new database and my all the collections was imported to it and once again, only the product collection doesn’t

//////////////////////////////////

/* require('dotenv').config({ path: '/.env' }) */
const path = require('path')
require('dotenv').config({ path: path.resolve(__dirname, '..', '.env') })
console.dir(process.env.MONGO_URI)
const mongoose = require('mongoose')

const connectDB = async () => {
  try {
    mongoose.connect(process.env.MONGO_URI, {
      useCreateIndex: true,
      useNewUrlParser: true,
      useUnifiedTopology: true,
    })
    console.log('MongoDB connection SUCCESS')
  } catch (error) {
    console.error('MongoDB connection FAIL')
    process.exit(1)
  }
}
console.dir(process.env.MONGO_URI)

module.exports = connectDB

////////////////////////////////////////////////////////////////

require('dotenv').config()

const productsData = require('./data/products')
const connectDB = require('./config/db')
const Product = require('./models/product')

connectDB()

const importData = async () => {
  try {
    /*  Product.deleteMany({}) */
    Product.insertMany(productsData)
    console.dir('Data Imported Successfuly')

    process.exit()
  } catch (error) {
    console.log(error)
    console.error('Error Ocured In Imported Data Process', error)
    process.exit(1)
  }
}

importData()

my model schema

 const mongoose = require('mongoose')
const products = require('../data/products')
const productSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  description: {
    type: String,
    required: true,
  },
  price: {
    type: Number,
    required: true,
  },
  countInStock: {
    type: Number,
    required: true,
  },
  imageUrl: {
    type: String,
    required: true,
  },
})

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

my code and terminal image

2

Answers


  1. Try this to create your schema instead

    const { Schema } = mongoose;
    
    const productSchema = new Schema({
      name: {
        type: String,
        required: true,
      },
      description: {
        type: String,
        required: true,
      },
      price: {
        type: Number,
        required: true,
      },
      countInStock: {
        type: Number,
        required: true,
      },
      imageUrl: {
        type: String,
        required: true,
      },
    })
    
    const Product = mongoose.model("Product", productSchema);
    Product.createCollection();
    
    Login or Signup to reply.
  2. Product.insertMany(productsData) returns a promise, but you aren’t waiting for that promise to finish before exiting the process. Add an await before it and you should be okay.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search