我是新來的節點,來自Ruby on Rails,並且我試圖建立一個如下的數據庫: 用戶有許多組和帖子。每個組都屬於一個用戶。每個帖子屬於一個用戶和一個或多個組。設置與mongoDB,node和angular.js的關係
爲此,我認爲最好使用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();
});
};
您好ODelibalta,非常感謝您的信息。我實際上是通過Dan Wahlin角色課程,以及Anthony Alicea的一個nodejs課程。 請允許我澄清問題。我主要遇到數據庫中的關係問題。我知道如何設置它,但是,我在發佈到數據庫時遇到了問題,比如說,創建屬於某個用戶的帖子,並且屬於特定的組。我的主要問題是我不知道代碼是什麼樣的,我無法找到完整的示例來顯示這樣做的代碼。 謝謝, –
我修改了我的問題,使其更清晰。它可以發現[這裏:](http://stackoverflow.com/questions/33789845/database-relationships-with-mongodb-how-to-post-to-database-using-angularjs) –