1
TypeError: Cannot call method 'call' of undefined
    at Array.MongooseArray._cast (/Users/bodo/Documents/Repositories/EnabreNode/node_modules/mongoose/lib/types/array.js:78:15)
    at Object.map (native)
    at Array.MongooseArray.push (/Users/bodo/Documents/Repositories/EnabreNode/node_modules/mongoose/lib/types/array.js:187:23)
    at module.exports (/Users/bodo/Documents/Repositories/EnabreNode/accounts/routes/me.js:29:27)
    at callbacks (/Users/bodo/Documents/Repositories/EnabreNode/node_modules/express/lib/router/index.js:160:37)
    at Promise.module.exports (/Users/bodo/Documents/Repositories/EnabreNode/middleware/load_user.js:15:13)
    at Promise.addBack (/Users/bodo/Documents/Repositories/EnabreNode/node_modules/mongoose/lib/promise.js:128:8)
    at Promise.EventEmitter.emit (events.js:96:17)
    at Promise.emit (/Users/bodo/Documents/Repositories/EnabreNode/node_modules/mongoose/lib/promise.js:66:38)
    at Promise.complete (/Users/bodo/Documents/Repositories/EnabreNode/node_modules/mongoose/lib/promise.js:77:20)

I actually want to built a quite simple schema:

{
    username: "bodo",
    email: "[email protected]",
    contacts: [{ 
        user: { username: "john", email: "[email protected]" }, 
        messages: [
            { 
                sender: { username: "bodo", email: "[email protected]" },
                receiver: { username: "john", email: "[email protected]" },
                body: "How are you these days?"
            },
            { 
                sender: { username: "john", email: "[email protected]" },                                       
                receiver: { username: "bodo", email: "[email protected]" },
                body: "I am totally fine?"
            } 
        ]
    }]
};

The contacts attribute contains a contacts subdocument which holds the user object of the actual contact (as reference) and later on some meta data like hasAcceptedContactRequest, contactSince, etc. Additionally I also save messages subdocs which are from a chat. These message subdocs contain the message as body attribute and who has send or received it. The sender and receiver are again references to a full user object which can be populated.

So much in theory. I now have created these schemas:

UserSchema

var Mongoose = require('mongoose');
var ContactSchema = require('./contact');

var UserSchema = new Mongoose.Schema({

    username: { 
        type: String, 
        index: { unique: true, sparse: true }, 
        required: true, lowercase: true, trim: true
    },

    email: {
        type: String,
        index: { unique: true, sparse: true }, 
        required: true, lowercase: true, trim: true
    },

    contacts: [ContactSchema]

});

module.exports = Mongoose.model('User', UserSchema, 'Users');

ContactSchema

var Mongoose = require('mongoose');
var MessageSchema = require('./message');

var ContactSchema = new Mongoose.Schema({

    user: {
        ref: "User",
        type: Mongoose.Schema.Types.ObjectId
    },

    messages: [MessageSchema]

});

module.exports = ContactSchema;

MessageSchema

var MessageSchema = new Mongoose.Schema({

    body: {
        type: String
    },

    sender: {
        ref: "User",
        type: Mongoose.Schema.Types.ObjectId
    },

    receiver: {
        ref: "User",
        type: Mongoose.Schema.Types.ObjectId
    }

});

module.exports = ContactSchema;

That is the route

app.put('/me', loadUser, function(req, res, next) {
    var user = req.user;
    var body = req.body;

    if (body.contacts) {
        body.contacts.forEach(function(contact) {
            user.contacts.push({ user: contact });
        });
    }

    console.log(req.user);

    user.save(function(err, user) {
        if (err) return next(err);
        res.send(200);
    });
});

Solution: As johnyHK mentioned there was a problem using the module system of node. I have accidentally exported the schemas as models. You now see the corrected examples above. So they should work.

2 Answers 2

1

Assuming the order of your schema definitions is the same in your code, you need to reorder them so that the referenced schemas are fully defined when used.

So that would be:

  1. MessageSchema
  2. ContactSchema
  3. UserSchema
6
  • Hi, how do I order them? Currently the route requires user.js which requires contact.js which requires message.js or do you mean putting them all together?
    – bodokaiser
    Nov 17, 2012 at 13:44
  • So I merged the Schemas all together in one file and see there it works! So your idea is right I will search how to control module order
    – bodokaiser
    Nov 17, 2012 at 13:48
  • okay sorry I did not found anything usefull about that problem. Couly you provide an example how I say require() to wait until the module is loaded?
    – bodokaiser
    Nov 17, 2012 at 13:57
  • @kyogron As long as you've got your schemas split up into separate files as you describe and your require statements are at the top of the files, then that should work. require is synchronous.
    – JohnnyHK
    Nov 17, 2012 at 14:00
  • @kyogron Your example document at the top doesn't match your schema definitions which contain a lot of ObjectId references. Can you update your question to show the contents of body.contacts that you're pushing to the user doc?
    – JohnnyHK
    Nov 17, 2012 at 14:05
0

Try this:

   user : { type: Schema.ObjectId, ref: 'User', index: true }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.