【发布时间】:2015-11-19 00:44:23
【问题描述】:
我是 node 新手,来自 Ruby on Rails,我正在尝试像这样设置一个数据库: 用户有很多组和帖子。每个组属于一个用户。每个帖子都属于一个用户和一个或多个组。
为此,我认为最好使用embedded sub-documents 而不是references。
我了解这应该如何工作的总体概念,但是,我很难将各个部分组合在一起。我很好奇我应该如何设置数据库,编写 api,然后设置控制器。我已经列出了我到目前为止所拥有的。任何能指引我正确方向的事情都会非常有帮助。
谢谢
用户数据模型
// app/models/user.js
// load the things we need
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var Schema = mongoose.Schema;
var Post = require('./post');
var Group = require('./groups');
// define the schema for our user model
var userSchema = mongoose.Schema({
local : {
email : String,
password : String,
username : String,
group : {
name: String,
created : {type:Date, default: Date.now},
post: { url: String,
highlighted: String,
comment: String,
image: String,
group: String,
timeStamp: String,
description: String,
title: String,
created: {type:Date, default: Date.now}
}
},
created: {type:Date, default: Date.now}
}
});
userSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.local.password);
};
module.exports = mongoose.model('User', userSchema);
API
...
router.route('/users')
.post(function(req, res) {
var user = new User();
user.local.name = req.body.name;
user.local.email = req.body.email;
user.local.password = req.body.password;
user.local.posts = req.body.post;
user.local.groups = req.body.group;
user.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'user created!' });
});
})
.get(function(req, res) {
User.find(function(err, users) {
if (err)
res.send(err);
res.json(users);
});
});
router.route('/users/:user_id')
// get the post with that id (accessed at GET http://localhost:8080/api/users/:user_id)
.get(function(req, res) {
User.findById(req.params.user_id, function(err, user) {
if (err)
res.send(err);
res.json(user);
});
})
// update the user with this id (accessed at PUT http://localhost:8080/api/users/:user_id)
.put(function(req, res) {
// use our user model to find the user we want
User.findById(req.params.user_id, function(err, user) {
if (err)
res.send(err);
user.name = req.body.name;
user.local.email = req.body.email;
user.local.password = req.body.password;
user.local.posts = req.body.post;
user.local.groups = req.body.group;
// save the user
user.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'user updated!' });
});
});
})
// delete the user with this id (accessed at DELETE http://localhost:8080/api/users/:user_id)
.delete(function(req, res) {
User.remove({
_id: req.params.user_id
}, function(err, user) {
if (err)
res.send(err);
res.json({ message: 'Successfully deleted' });
});
});
...
控制器
$scope.addUser = function() {
//$scope.user.url = parenturl;
console.log($scope.user._id);
$http.user('/api/users', $scope.user).success(function(response){
refreshuser();
//$scope.user = {url: parenturl}
});
};
$scope.remove = function(id) {
console.log(id);
$http.delete('/api/users/' + id).success(function(response) {
//refreshuser();
});
return false;
};
$scope.edit = function(id){
console.log(id);
$http.get('/api/users/' + id).success(function(response) {
$scope.user = response;
});
};
$scope.update = function() {
console.log($scope.user.id);
$http.put('/api/users/' + $scope.user._id, $scope.post).success(function(response) {
refreshUser();
});
};
【问题讨论】:
标签: javascript angularjs node.js express mongoose