Installing mongoose friends plugin with MeanJS

2019-06-14 09:36发布

Thanks for your support!

Every time I run a Yeoman CRUD scaffold for mean.js, it breaks my app. The page turns completely white. So I'm not exactly sure which files to add where for CRUD functionality.

I'd like the ability for users to "friend" each other like on Facebook. There's this plugin called mongoose friends. It looks very promising. The problem is there's no example html page included with the git repository to walk me through installation. Is see there's a lib folder with plugin.js and status.js. Does that go server side or client side? I'm guessing it goes server side...

https://github.com/numbers1311407/mongoose-friends

How would I separate all this code out in the single js file into MVC for MeanJS?

I didn't want to post this question at first, because I thought the question might be asking too much or too broad, but then I realized it's a basic "How do I install a plugin?" question. New MeanJS users could benefit from this type of question.

1条回答
Melony?
2楼-- · 2019-06-14 10:07

Installing is easy via npm, simply:

npm install mongoose-friends --save

I have no experience with MEAN.JS, but it seems to be for the most part a collection of generators to create a CRUD-patterned angular/express app. As such it seems to follow the philosophy of the framework to create the friendship as a CRUD resource.

Using yo as they suggest:

yo meanjs:crud-module friendship

This will generate the MVC for a friendship model, but will make some incorrect assumptions about the model itself, namely that it is a first class mongoose model. With this plugin, it is not. Rather friendships are part of an embedded collection on the user record, the plugin provides CRUD methods for them.

First off, add the plugin to your user model.

// in app/models/user.server.model.js
var friends = require("mongoose-friends");
// ...
UserSchema.plugin(friends());

The generated model at app/models/friendship, and references to it in the generated files, will need to be removed. Instead of a Friendship model, frienships will be CRUD'd through the plugin methods added to your User model.

The controller generated at app/controllers/friendships.server.controller.js will likely require the most change.

create, for example would change from this:

var friendship = new Friendship(req.body);
friendship.user = req.user;
friendship.save(callback);

To something more like:

req.user.requestFriend(req.body.user, callback);

The routes may need to change as well, depending on how your application uses friendships. Friendships of the plugin are not a first-class resource, but rather an embedded collection of a user. As such there's no public /friendships route, for example. Either that route would need to return only the logged in users friends, or you'd want to map a friendship route specific to the user, e.g. /users/ID/friendships, in the case where the friendships of a user were viewable by those other than the user itself.

Anyway, this is no doubt woefully incomplete and perhaps even misguided, but I hope it's enough to get you started on the implementation.

查看更多
登录 后发表回答