Friday, 21 April 2017

Mongoose Error trying to create seed for database

I am new to Mongoose/MongoDB/Express and I have done research and still ending up in same spot with the errors. What I am trying to do is referencing between three schemas (User, Subs, Business). User Schema

   (users.js)
        var mongoose = require('mongoose');
        var userSchema = new mongoose.Schema({
            username: String,
            password: String,
            subs: [
                    {
                        type: mongoose.Schema.Types.ObjectId,
                        ref: 'Business'
                    }
            ]
        });
        module.exports = mongoose.model('User', userSchema);

Business Schema

   (business.js)
    var mongoose = require('mongoose');
    var busSchema = new mongoose.Schema({
       name: String,
       type: String,
       logo: String,
       desc: String,
       loca: {
           street: String,
           city: String,
           state: String,
           zipcode: Number
       },
       comments: [
          {
             type: mongoose.Schema.Types.ObjectId,
             ref: 'Comment'
          }   
       ],
       created: {
           type: Date, default: Date.now
       }
    });
    module.exports = mongoose.model('Business', busSchema);

Subs Schema

 var mongoose = require('mongoose');
    var subSchema = new mongoose.Schema(
        {
            id: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'Business'
            }
        }  
    );

    module.exports = mongoose.model('Subs', subSchema);

I am fairly certain that the problem lies in the User schema and Subs. I couldnt use .push() nor .save(). Am I missing something or have the reference flow wrong. This is the seed file that I am trying to attach an exisiting business id to subs and save the id to users.

var mongoose = require('mongoose');
var User = require('./users');
var Business = require('../business/business');
var Subs = require('../meta/subs');


function seedUser() {
    User.remove({}, function(err) {
        if(err) {
            console.log(err);
        }
        console.log('User Database has been cleared.');
        User.create(
            {
                username: 'JohnDoes',
                password: 'password'
            }, function(err, user) {
                if(err) {
                    console.log(err);
                } else {
                    Business.find({'name':'Galaxy Man'}, function(err, bus) {
                        if(err) {
                            console.log(err);
                        } else {
                            Subs.create(bus, function(err, sub) {
                                if(err) {
                                    console.log(err);
                                } else {
                                    console.log(sub);
                                    user.subs.push(sub);
                                    user.save();
                                }
                            });
                        }
                    });
                }
            }
        );
    });
}
//WIPE DB
module.exports = seedUser;

Sorry this is my first question and hopefully I have it explained clearly.



via Matthew Restine

No comments:

Post a Comment