reaper_unique
reaper_unique

Reputation: 2931

MongoDB creates empty document

The following code worked like a charm just 30 minutes ago but after trying to add a Date to my Schema, it suddenly stopped working and now I always add an empty document to my collection.

The only thing I get is:

{ "_id" : ObjectId("564e1e7681c448840e06caea"), "__v" : 0 }

Can anyone help me pinpoint the fault in my code?

image.js

var mongoose = require('mongoose');
Schema = mongoose.Schema;

var imagesSchema = new Schema({
    auctionId: String,
    name: String,
    dateAdded: Date
});

mongoose.exports = imagesSchema;

auction.js

var mongoose = require('mongoose');
var imagesSchema = require('./image.js');
var Image = mongoose.model('Image', imagesSchema);

Schema = mongoose.Schema;

var auctionsSchema = new Schema({
    name: String,
    address:
    {
        street: String,
        number: Number,
        box: String,
        postalCode: Number,
        city: String,
    },  
    auctionDate: Date, 
    finished: Boolean,
    viewingCode: String,
    images: [{ type: Schema.Types.ObjectId, ref: 'Image' }]
});

mongoose.exports = auctionsSchema;

The below operation is called upon initialization of my home.html page. So the fault must be somewhere in either the below operation or the above schema design.

MongoOperations.js

var mongoose = require('mongoose');
var auctionSchema = require('./auction.js');

mongoose.connect("mongodb://localhost/auctionsDb");
var db = mongoose.connection;

db.on('error', console.error.bind(console, "connection error"));
var Auction = db.model('Auction', auctionSchema);

//Once a connection is made to the DB, log this,
//find the db and if there are no results in the db, create a basic entry
db.once('open', function () {
    console.log("auctionsDb is open...");

    Auction.find().exec(function (error, results) {
        if (results.length === 0) {
            Auction.create({
                name: "Zaal Hand in Hand",  
                address: {
                    street: "Street Anon", 
                    number: 317,
                    box: "AB",
                    postalCode: 9490,
                    city: "Anonty"
                }, 
                auctionDate: new Date(),
                finished: true,
                viewingCode: "",
                images: [{}]
            });
        }
    });
});

Upvotes: 2

Views: 3093

Answers (2)

Scott Stensland
Scott Stensland

Reputation: 28285

Always do error checking. Your document insert fails with

message: 'Cast to Array failed for value "[object Object]" at path "images"',

Whenever you have

xxxx(function(error, results){ {

without exception always do something similar to

xxxx(function(error, results){ {

    if (error) {
        console.log('save seeing error');
        console.log(error);
        return;
    }

Simple work around is to just null out your Image Reference :

Auction.create({
    name: "Zaal Hand in Hand",  
    address: {
        street: "Street Anon", 
        number: 317,
        box: "AB",
        postalCode: 9490,
        city: "Anonty"
    }, 
    auctionDate: new Date(),
    finished: true,
    viewingCode: "",
    // images: [{}]     
    images: null
});

Upvotes: 1

Abdullah Rasheed
Abdullah Rasheed

Reputation: 3752

You have a few issues, but the major one is that it looks like you may need to change your lines that read mongoose.exports to module.exports. You aren't exporting your Schema.

Upvotes: 4

Related Questions