-1

私は友情ベースの関係をスキーマに追加するモジュールに取り組んでいます。

私は基本的に、この男がやろうとしていることをやろうとしています(私の知る限り、うまくいくはずです-これは落胆しています)

がコールバックに到達find(...)しないのはなぜですか?FriendshipSchema.statics.getFriends

編集- 予想される実行フローを説明させてください...

内側accounts.js

  1. 「friends-of-friends」モジュール (loads friends-of-friends/index.js) が 必要です。
    1. を作成し、静的メソッドを追加し、モデルを返すfriends-of-friends/friendship.js関数をエクスポートする必要があります。FriendshipSchemaFriendship
    2. friends-of-friends/plugin.js静的メソッドとインスタンス メソッドを AccountSchema に追加する mongoose プラグインをエクスポートする必要があります。
  2. から機能をプラグインするためにFriendsOfFriends.plugin( を参照) を使用します。friends-of-friends/index.jsfriends-of-friends/plugin.js
  3. を定義AccountSchema.statics.searchしますthis.getFriends。はコンパイルされたモデルを参照し、プラグインが追加された
    ため、内での呼び出しは inで定義されているように呼び出されます。thisAccountschema.statics.getFriendsthis.getFriendsAccountSchema.statics.searchschema.statics.getFriendsfriends-of-friends/plugin.jsFriendship.getFriendsFriendshipSchema.statics.getFriendsfriends-of-friends/friendship.jsthis.find(...)
  4. アカウント ドキュメントを取得した後、 を呼び出しaccount.search('foo', function (...) {...});ますが、 でわかるようにFriendshipSchema.statics.getFriendsfindメソッドは実行されますが、そのコールバックは呼び出されず、プログラムがハングします :(

エラーは発生しないので、これが論理的な問題であることはわかっていますが、なぜハングアップするのかわかりません...

編集- 以下の私の回答を参照してください。モデルを呼び出す前に、モデルをコンパイルする必要もありfindました。

account.js

var mongoose = require('mongoose'),
    passportLocalMongoose = require('passport-local-mongoose');

var FriendsOfFriends = require('friends-of-friends')();

// define the AccountSchema
// username, password, etc are added by passportLocalMongoose plugin
var AccountSchema = new mongoose.Schema({
    created:        { type: Date,       default:    Date.now                    },
    profile: {
        displayName:    { type: String,     required:   true,       unique : true,  index: true     },
        firstName:      { type: String,     required:   true,       trim: true,     index: true     }, 
        lastName:       { type: String,     required:   true,       trim: true,     index: true     }, 
    }
});

// plugin the FriendsOfFriends plugin to incorporate relationships and privacy
AccountSchema.plugin(FriendsOfFriends.plugin, FriendsOfFriends.options);

AccountSchema.statics.search = function (userId, term, done) {
    debug('search')

    var results = {
            friends: [],
            friendsOfFriends: [],
            nonFriends: []
        },
        self=this;

    this.getFriends(userId, function (err, friends) {

       // never reaches this callback!

    });

};

AccountSchema.methods.search = function (term, done) {
    debug('method:search')
    AccountSchema.statics.search(this._id, term, done);
};

module.exports = mongoose.model('Account', AccountSchema);

友達の友達/index.js

/**
 * @author  Jeff Harris
 * @ignore
 */

var debug = require('debug')('friends-of-friends');
    friendship = require('./friendship'),
    plugin = require('./plugin'),
    privacy = require('./privacy'),
    relationships = require('./relationships'),
    utils = require('techjeffharris-utils');

module.exports = function FriendsOfFriends(options) {

    if (!(this instanceof FriendsOfFriends)) {
      return new FriendsOfFriends(options);
    } 

    var defaults = {
        accountName:    'Account',
        friendshipName: 'Friendship', 
        privacyDefault: privacy.values.NOBODY
    };

    this.options = utils.extend(defaults, options);

    /**
     * The Friendship model
     * @type {Object}
     * @see  [friendship]{@link module:friendship}
     */
    this.friendship = friendship(this.options);

    /**
     * mongoose plugin
     * @type {Function}
     * @see  [plugin]{@link module:plugin}
     */
    this.plugin = plugin;

    debug('this.friendship', this.friendship);

};

友達の友達/friendship.js

var debug = require('debug')('friends-of-friends:friendship'),
    mongoose = require('mongoose'),
    privacy = require('./privacy'),
    relationships = require('./relationships'),
    utils = require('techjeffharris-utils');

module.exports = function friendshipInit(options) {

    var defaults = {
        accountName:    'Account',
        friendshipName: 'Friendship',
        privacyDefault: privacy.values.NOBODY
    };

    options = utils.extend(defaults, options);

    debug('options', options);

    var ObjectId = mongoose.Schema.Types.ObjectId;

    var FriendshipSchema = new mongoose.Schema({
        requester: { type: ObjectId, ref: options.accountName, required: true, index: true },
        requested: { type: ObjectId, ref: options.accountName, required: true, index: true },
        status: { type: String, default: 'Pending', index: true},
        dateSent: { type: Date, default: Date.now, index: true },
        dateAccepted: { type: Date, required: false, index: true }
    });

    ...

    FriendshipSchema.statics.getFriends = function (accountId, done) {
        debug('getFriends')

        var model = mongoose.model(options.friendshipName, schema),
            friendIds = [];

        var conditions = { 
            '$or': [
                { requester: accountId },
                { requested: accountId }
            ],
            status: 'Accepted'
        };

        debug('conditions', conditions);

        model.find(conditions, function (err, friendships) {
            debug('this callback is never reached!');

            if (err) {
                done(err);
            } else { 
                debug('friendships', friendships);

                friendships.forEach(function (friendship) {
                    debug('friendship', friendship);

                    if (accountId.equals(friendship.requester)) {
                        friendIds.push(friendship.requested);
                    } else {
                        friendIds.push(friendship.requester);
                    }

                });

                debug('friendIds', friendIds);

                done(null, friendIds);
            }

        });

        debug('though the find operation is executed...');
    };

    ...

    return mongoose.model(options.friendshipName, FriendshipSchema);
};

友達の友達/plugin.js

var debug = require('debug')('friends-of-friends:plugin'),
    mongoose = require('mongoose'),
    privacy = require('./privacy'),
    relationships = require('./relationships'),
    utils = require('techjeffharris-utils');

module.exports = function friendshipPlugin (schema, options) {

    var defaults = {
        accountName:    'Account',
        friendshipName: 'Friendship',
        privacyDefault: privacy.values.NOBODY
    };

    options = utils.extend(defaults, options);

    var Friendship = mongoose.model(options.friendshipName);

    ...

    schema.statics.getFriends = function (accountId, done) {
        debug('getFriends')

        var model = mongoose.model(options.accountName, schema);

        var select = '_id created email privacy profile';

        Friendship.getFriends(accountId, function (err, friendIds) {
            if (err) {
                done(err);
            } else {
                model.find({ '_id' : { '$in': friendIds } }, select, done);
            }
        });
    };

    ...

    schema.methods.getFriends = function (done) {
        schema.statics.getFriends(this._id, done);
    };
};
4

1 に答える 1

0

この問題は、マングースのどのインスタンスが必要であったかに関連していました。

私のメインアプリ内で、私は mongoose from を要求していましたapp/node_modules/mongooseが、私のfriends-of-friendsモジュール (依存関係としてpackage.jsonmongoose をリストしていた) は から mongoose を要求しapp/node_modules/friends-of-friends/node_modules/mongooseていました。

依存関係として mongoose を削除し、ネストされたnode_modulesフォルダーを削除し、vioala も動作します :)

RTFMが必要です

app/
|   lib/
|   node_modules/ 
|   |   mongoose/             <-- main app required here
|   |   friends-of-friends/
|   |   |   node_modules/     <-- deleted; mongoose was only dep
|   |   |   |   mongoose/     <-- friends-of-friends module required here
|   server.js
于 2014-12-16T11:18:36.997 に答える