Installing mongoose friends plugin with MeanJS

假如想象 提交于 2019-12-23 04:52:13

问题


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:


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.



来源:https://stackoverflow.com/questions/24084578/installing-mongoose-friends-plugin-with-meanjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!