我是来自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();
});
};
答案 0 :(得分:0)
我认为你做得很好。比我开始时肯定的那样好得多。我不确定这里的问题是什么,但如果您正在寻找MEAN路径上的其他资源,首先我建议使用framework它会减少一些开发时间。还有很多其他选择。
我也read this book我发现它简短而又可爱。本书从零开始,最终在heroku上实时部署应用程序。本书的作者还管理一个名为scotch.io的网站,你会发现一些关于MEAN堆栈的好教程。
如果您想了解更多javascript,一般还有egghead.io。他们有免费和付费的教程。即使是免费的也有时好。
如果您想了解更多有关mongo的信息,请务必参加mongo university的课程。他们是免费的,而且很棒。到目前为止,我已经完成了4门课程,如果你通过了课程,你将获得完成证书。拥有证书对我来说无疑是激励,也可能会激励你。
如果您想了解有关角度本身的更多信息,我强烈推荐关于udemy的Dan Wahlin's课程。我从他那里拿走了两个,他们已经照亮了一盏灯,让我更有意义。如果你不想支付全价,我建议等几周,直到在美国感恩节。很多在线交易发生在那个时间。我在一笔交易中以每张10美元或类似的东西参加了这些课程。
我是一名PHP开发人员已有6年多。感谢nodejs,我很确定JavaScript是未来。多数民众赞成我的个人意见,我只是提到它,希望它会给你勇气,绝对不会开始讨论战争。我希望我帮忙。