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)

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

{
    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?"
            } 
        ]
    }]
};

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

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

用户模式

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;

消息模式

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所说,使用node的模块系统存在问题。我不小心将模式导出为模型。您现在可以看到上面更正后的示例。所以他们应该工作。

4

2 回答 2

1

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

所以那将是:

  1. MessageSchema
  2. ContactSchema
  3. UserSchema
于 2012-11-17T13:37:30.403 回答
0

试试这个:

   user : { type: Schema.ObjectId, ref: 'User', index: true }
于 2012-11-17T09:33:23.660 回答