Mongoose subdocs:无法调用未定义的方法调用

时间:2012-11-17 09:23:48

标签: javascript mongodb reference mongoose document

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)

我实际上想构建一个非常简单的架构:

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

contacts属性包含一个contacts子文档,它保存实际联系人的用户对象(作为参考),稍后包含一些元数据,如hasAcceptedContactRequest,contactSince等。 另外,我还保存了来自聊天的消息子目录。这些消息子目录包含作为正文属性的消息以及发送或接收它的人。发件人和收件人再次引用可以填充的完整用户对象。

理论上这么多。我现在已经创建了这些模式:

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;

这是路线

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);
    });
});

解决方案: 正如johnyHK所说,使用节点的模块系统存在问题。我不小心 将模式导出为模型。您现在可以看到上面更正的示例。所以他们应该工作。

2 个答案:

答案 0 :(得分:1)

假设您的模式定义的顺序在您的代码中是相同的,您需要重新排序它们,以便在使用时完全定义引用的模式。

那就是:

  1. MessageSchema
  2. ContactSchema
  3. UserSchema

答案 1 :(得分:0)

试试这个:

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