私は友情ベースの関係をスキーマに追加するモジュールに取り組んでいます。
私は基本的に、この男がやろうとしていることをやろうとしています(私の知る限り、うまくいくはずです-これは落胆しています)
がコールバックに到達find(...)
しないのはなぜですか?FriendshipSchema.statics.getFriends
編集- 予想される実行フローを説明させてください...
内側accounts.js
:
- 「friends-of-friends」モジュール (loads
friends-of-friends/index.js
) が 必要です。- を作成し、静的メソッドを追加し、モデルを返す
friends-of-friends/friendship.js
関数をエクスポートする必要があります。FriendshipSchema
Friendship
friends-of-friends/plugin.js
静的メソッドとインスタンス メソッドを AccountSchema に追加する mongoose プラグインをエクスポートする必要があります。
- を作成し、静的メソッドを追加し、モデルを返す
- から機能をプラグインするために
FriendsOfFriends.plugin
( を参照) を使用します。friends-of-friends/index.js
friends-of-friends/plugin.js
- を定義
AccountSchema.statics.search
しますthis.getFriends
。はコンパイルされたモデルを参照し、プラグインが追加された
ため、内での呼び出しは inで定義されているように呼び出されます。this
Account
schema.statics.getFriends
this.getFriends
AccountSchema.statics.search
schema.statics.getFriends
friends-of-friends/plugin.js
Friendship.getFriends
FriendshipSchema.statics.getFriends
friends-of-friends/friendship.js
this.find(...)
- アカウント ドキュメントを取得した後、 を呼び出し
account.search('foo', function (...) {...});
ますが、 でわかるようにFriendshipSchema.statics.getFriends
、find
メソッドは実行されますが、そのコールバックは呼び出されず、プログラムがハングします :(
エラーは発生しないので、これが論理的な問題であることはわかっていますが、なぜハングアップするのかわかりません...
編集- 以下の私の回答を参照してください。モデルを呼び出す前に、モデルをコンパイルする必要もあり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);
};
};