简体   繁体   English

Mongoose中用于Node.js的带有自定义对象的嵌套模式

[英]Nested Schemas with Custom Objects in Mongoose for Node.js

I'm stuck on trying to use Mongoose for my simple Node.js app. 我一直试图在我的简单Node.js应用程序中使用Mongoose。 I have created a couple custom objects in my object that basically implement a linked list data structure. 我在对象中创建了几个自定义对象,这些对象基本上实现了链表数据结构。 Ive been able to save the objects successfully but the problem comes when I try to retrieve the object and call some of the methods that I have defined for it. 我已经能够成功保存对象,但是当我尝试检索对象并调用为它定义的某些方法时,问题就来了。 I tried using Object.create() as a workaround but that did not work. 我尝试使用Object.create()作为解决方法,但这没有用。 Here is where the code fails: 这是代码失败的地方:

UserObj.findOne( { username : username } ).exec( function(err, user) {
    if(err) {
        res.status(404).json( {success: false, message: "Database error"} );
        return;
    }
    if(user == null) {
        res.status(404).json( {success: false, message: "Username not Registered"} );
        return;
    }
    var sq = Object.create(MusicQueue, user.songQueue); // Not sure if im supposed to pass in MusicQueue or MusicQueue.prototype. Neither work though
    var pl = sq.getPlaylist();
    res.json({
        username : username,
        lastLogin : user.lastLogin,
        playlist : pl
    });
});

And here is the error I am getting: 这是我得到的错误:

    var pl = sq.getPlaylist();
                ^
TypeError: Object object has no method 'getPlaylist'

I do have that method defined as part of the prototype in the MusicQueue object ( MusicQueue.prototype.getPlaylist = function() {...}; ). 我确实在MusicQueue对象中定义了该方法作为原型的一部分( MusicQueue.prototype.getPlaylist = function() {...}; )。

Any ideas on how I can get around this? 关于如何解决这个问题的任何想法? Is there a way to cast user.songQueue from a generic object to a MusicQueue object and be able to call its instance methods? 有没有一种方法可以将user.songQueue从通用对象转换为MusicQueue对象,并能够调用其实例方法?

Your User Schema should be defined like this: 您的User架构应按以下方式定义:

userSchema.methods.getPlayList = function () {
  // * your function*, might need a callback if async...
};

var User = mongoose.Model('User', userSchema);

You can add a static method by using: 您可以使用以下方法添加static方法:

userSchema.statics.getUsersById = function(cb) {
  // * do something, and call cb when done, assuming it's async
};

You shouldn't use prototype to add methods. 您不应该使用prototype添加方法。 Mongoose will create an instance of the User model for result of calling findOne , and the getPlayList function, if properly defined as shown above, will be available (it was added to the prototype). 猫鼬将为调用findOne结果创建User模型的实例,并且getPlayList函数(如上所示正确定义)将可用(它已添加到原型中)。

You mentioned in your comment a Linked List. 您在评论中提到了链接列表。 You could do that like this: 您可以这样做:

var nodeSchema = mongoose.Schema({
    value: String,
    link: { type:  mongoose.Schema.Types.ObjectId, ref: 'Node' }
});

var Node = mongoose.Model('Node', nodeSchema);

I got around my particular situation by just creating copy(obj) methods to my objects that will take the objects returned by the findOne(...) method and copy the data in it to my objects. 我只是通过为我的对象创建copy(obj)方法来解决我的特殊情况,该方法将使用findOne(...)方法返回的对象并将其中的数据复制到我的对象中。 I've come up with other problems as a result of this but Im pretty sure they are unrelated to this question. 因此,我提出了其他问题,但是我很确定它们与该问题无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM