Filling collection with schema data problem

Hello

Im current in fullstack course and Im having problem appropriating a framework for seeding data inside collections. It creates consumer and tickets collections however json to schema data does not get sent / get reject. Im quite green.

Seeder.js

const fs = require('fs')
const path = require('path')
const db = require('../server/ModelHandler')
const mongoose = require('mongoose')

module.exports = class Seeder {
  static async seed() {
    await db.connect()
    await mongoose.connection.db.dropDatabase()
    console.log('\n\nSEEDING DB\n' + '-'.repeat(60) + '\n')
    await this.insertData()
    console.log('\n\nAll done!\n');
    process.exit();
  }

  static async insertData() {
    let allCustomers = []
    let data = this.readJsonFiles('./', 'data')
    
    for (let [route] of data) {
      let model = db.modelsByApiRoute[route]._model;
      if (!model) {
        throw (new Error(`Model for collection ${route} not found.`));
      }
      if (route === 'customers') {
        console.log('customers working')
        rows.forEach(x =>
          x.customerId = x.customerId === null ? null : allCustomers[x.customerId - 1]); 
      } 
    
      console.log(`Inserted data in the ${route} collection.`);
    }
  }
}``` 

ModelHandler.js / seems to format the json to schema via a template. 

const fs = require(‘fs’)

const path = require(‘path’)

const mongoose = require(‘mongoose’)

module.exports = class ModelHandler {

static types = mongoose.Schema.Types

static modelsByApiRoute = {}

static modelsByName = {}

static async connect() {

global.db = this

await mongoose.connect(require('./secrets/dbCredentials.json'))

this.getAllModels()

}

static getAllModels() {

let modelsPath = path.join(__dirname, 'models')

fs.readdirSync(modelsPath).sort()

  .filter(x => x.slice(-3) === '.js')

  .map(x => path.join(modelsPath, x))

  .forEach(x => require(x))

this.finalizeSchemasAndModels()

}

static registerModel(settings) {

this.modelsByApiRoute[settings.apiRoute] = settings

this.modelsByName[settings.model] = settings

}

static finalizeSchemasAndModels() {

for (let x of Object.values(this.modelsByName)) {

  let { collection } = x;

  let schemaProps = x.schemaProperties

    || this.modelsByName[x.schemaPropertiesFrom].schemaProperties;

  let schema = new mongoose.Schema(schemaProps, { collection });

  x.addHooks && x.addHooks(schema);

  let model = mongoose.model(x.model, schema);

  console.log('Model ' + x.model + ' created');

  x._model = model;

}

}

}


The data itself 

json - > template 

[

{

"customerName": "aderges0@cnet.com",

"email": "Arleta",

"telephoneNumber": "16"

},

{

"customerName": "aderges0@cnet.com",

"email": "Arleta",

"telephoneNumber": "16"

},

{

"customerName": "aderges0@cnet.com",

"email": "Arleta",

"telephoneNumber": "6"

}

]


the template 
customer for example 

db.registerModel({

model: ‘Customer’,

collection: ‘customers’,

apiRoute: ‘customers’,

readOnly: false,

schemaProperties: {

customerName: { type: String, required: true },

email: { type: String, required: true },

telephoneNumber: {type: String, required: true}

},

addHooks(schema) { }

})

Oh by the way - this post is horrible formatted; excuse me while I try to figure out how to edit my post !

Sorry !

Well this is certainly not the first impression I wanted to give - but I cant edit my first post.

Im abit afraid to post now but - here is the related code I have to illustrate my problem.

Seeder.js
const fs = require('fs')
const path = require('path')
const db = require('../server/ModelHandler')
const mongoose = require('mongoose')

module.exports = class Seeder {
  static async seed() {
    await db.connect()
    await mongoose.connection.db.dropDatabase()
    console.log('\n\nSEEDING DB\n' + '-'.repeat(60) + '\n')
    await this.insertData()
    console.log('\n\nAll done!\n');
    process.exit();
  }

  static async insertData() {
    let allCustomers = []
    let data = this.readJsonFiles('./', 'data')
    
    for (let [route] of data) {
      let model = db.modelsByApiRoute[route]._model;
      if (!model) {
        throw (new Error(`Model for collection ${route} not found.`));
      }
      if (route === 'customers') {
        console.log('customers working')
        rows.forEach(x =>
          x.customerId = x.customerId === null ? null : allCustomers[x.customerId - 1]); 
      } 
    
      console.log(`Inserted data in the ${route} collection.`);
    }
  }
}

/// ---------
ModelHandler.js 

const fs = require('fs')
const path = require('path')
const mongoose = require('mongoose')

module.exports = class ModelHandler {
  
  static types = mongoose.Schema.Types
  static modelsByApiRoute = {}
  static modelsByName = {}

  static async connect() {
    global.db = this
    await mongoose.connect(require('./secrets/dbCredentials.json'))
    this.getAllModels()
  }

  static getAllModels() {
    let modelsPath = path.join(__dirname, 'models')
    fs.readdirSync(modelsPath).sort()
      .filter(x => x.slice(-3) === '.js')
      .map(x => path.join(modelsPath, x))
      .forEach(x => require(x))
    this.finalizeSchemasAndModels()
  }

  static registerModel(settings) {
    this.modelsByApiRoute[settings.apiRoute] = settings
    this.modelsByName[settings.model] = settings
  }

  static finalizeSchemasAndModels() {
    for (let x of Object.values(this.modelsByName)) {
      let { collection } = x;
      let schemaProps = x.schemaProperties
        || this.modelsByName[x.schemaPropertiesFrom].schemaProperties;
      let schema = new mongoose.Schema(schemaProps, { collection });
      x.addHooks && x.addHooks(schema);
      let model = mongoose.model(x.model, schema);
      console.log('Model ' + x.model + ' created');
      x._model = model;
    }
  }
}

/// ------ The data 

customers.json 

[
  {
    "customerName": "aderges0@cnet.com",
    "email": "Arleta",
    "telephoneNumber": "16"
  },
  {
    "customerName": "aderges0@cnet.com",
    "email": "Arleta",
    "telephoneNumber": "16"
  },
  {
    "customerName": "aderges0@cnet.com",
    "email": "Arleta",
    "telephoneNumber": "6"
  }
]

/// ----- The customer template  (unsure if this is the correct technical term ) Im trying !

db.registerModel({
  model: 'Customer',
  collection: 'customers',
  apiRoute: 'customers',
  readOnly: false,
  schemaProperties: {
    customerName: { type: String, required: true },
    email: { type: String, required: true },
    telephoneNumber: {type: String, required: true}
  },
  addHooks(schema) { }
}) 

It does create the collections , but for costumers where there should be data - there is none.

Here is some additional information / development on the problem.

Excuse the self replies !

Hi @Tim_Hogklint welcome to the community!

If I understand correctly, the issue you’re having is that you’re trying to seed the database with the example data you have (marked as customers.json there) but you’re not seeing the data in the collection. Is this correct?

Does this mean that the file seeder.js is not doing what it’s supposed to do? In mongoose, typically you call the save() method to save the data in the database. However in the code you posted, I see none of them. Without save() or any other method that does a similar thing, definitely there won’t be any data in the database :wink:

Having said that, it’s a bit difficult to confirm what you’re seeing since I think the code you posted forms part of a larger code. I suggest you trim the code down to something that can be executed as-is without requiring any other infrastructure. This is so that: 1) Anyone can copy-paste the code and run it, 2) Proof that it’s not caused by any misconfiguration on the infrastructure part of the code, and 3) Illustrates the problem clearly and succintly.

Do you mind trimming the code down so it can achieve all three points above?

Best regards
Kevin

1 Like

Thanks for taking the time to try and help me !

There has been an update ; I can via this set data in the customer collection via vs code tool

Now I looking to transfer that to our actual rest api if that makes sense

I dont really understand how the “data” goes from

const fs = require('fs')
const path = require('path')
const mongoose = require('mongoose')

module.exports = class ModelHandler {
  
  static types = mongoose.Schema.Types
  static modelsByApiRoute = {}
  static modelsByName = {}

  static async connect() {
    global.db = this;
    await mongoose.connect(require('./secrets/dbCredentials.json'))
    this.getAllModels()
  }

  static getAllModels() {
    let modelsPath = path.join(__dirname, 'models')
    fs.readdirSync(modelsPath).sort()
      .filter(x => x.slice(-3) === '.js')
      .map(x => path.join(modelsPath, x))
      .forEach(x => require(x))
    this.finalizeSchemasAndModels()
  }

  static registerModel(settings) {
    this.modelsByApiRoute[settings.apiRoute] = settings
    this.modelsByName[settings.model] = settings
  }

  static finalizeSchemasAndModels() {
    for (let x of Object.values(this.modelsByName)) {
      let { collection } = x;
      let schemaProps = x.schemaProperties
        || this.modelsByName[x.schemaPropertiesFrom].schemaProperties;
      let schema = new mongoose.Schema(schemaProps, { collection });
      x.addHooks && x.addHooks(schema);
      let model = mongoose.model(x.model, schema);
      console.log('Model ' + x.model + ' created');
      x._model = model;
    }
  }
}

Hope this illustrates the problem ?
Otherwise I have the example the teacher gave us where its isolated to just the
bare component , he was not done with the rest api - I guess; Im not sure how to
continue “the work” I guess ?
Isolated code zip download