2016-04-15 119 views
0


对不起,我的英语不好。
我用的node.js + express.js + mongoose.js 我有这个模式中猫鼬适合群体:
Mongoose更新引用对象?

var groupSchema = new mongoose.Schema({ 
    name: String, 
    users: [{type: mongoose.Schema.ObjectId, ref: 'User'}], 
    posts: [{type: mongoose.Schema.ObjectId, ref: 'Post'}] 
}); 

和这个模式给用户:

var userSchema = new mongoose.Schema({ 
    login: { type: String, unique: true, lowercase: true }, 
    password: String, 
    unread: [{type: mongoose.Schema.ObjectId, ref: 'Post'}]  
}); 

集团的名单与该组相关的用户以及与该组相关的帖子列表。
我想要实现的是:
Group1有用户Mike,JohnJane;
当用户Mike创建一个新的职位:
1)我觉得目前的组,然后选择用户,与该组(Group1和用户MikeJohnJane);

2)给用户JohnJane我必须在unread字段中设置创建的帖子。 (让这个知道,哪个用户还没有阅读)。
它是正确的吗?如果是的话,我如何更新参考文件中的这个未读字段?
我试着这样做:
在例如:组网址:http://localhost:3000/group/first

app.get('/group/:name', groups.getGroupPage); 

    app.post('/group/:name', posts.postCreate); 

Posts.js

var Group = require('../models/group'); 
var User = require('../models/user'); 
var Post = require('../models/post'); 

     exports.postCreate = function(req, res) { 
     var post = new Post({ 
      title: req.body.p_title, 
      content: req.body.p_content, 
      author: req.user 
     }); 
     Group 
        .update({ name: req.params.name }, {upsert:true}, { "$push": { "users.$.unread": post._id } }) 
        .populate('users') 
        .exec(function(err,group) { 
         if (err) res.json(err) 
         console.log(group); 
        } 
       ); 
     } 

感谢的任何帮助。

+0

请发表您自己试过的代码。我们需要更多信息来帮助您。你使用快递吗?如果是,那么您使用的路线是什么?你要求什么信息发送到快递?你是否发送了带有请求的用户名或用户ID? – Snekw

+0

增加了更多的代码,谢谢你的 – Ramil

回答

0

你的编码风格略有不同,从我的,但我会去这个方式如下:

exports.postCreate = function(req, res){ 
    //Create a new post 
    var newPost = new Post({ 
     title: req.body.p_title, 
     content: req.body.p_content, 
     author: req.user 
    }); 

    //Save the new post 
    newPost.save(function(err){ 
     if(err){ 
      res.json(err); 
     } 
    }); 

    //Find the group with the name and populate the users field 
    Group.findOne({'name': req.params.name}).populate('users').exec(function(err, group){ 
     if(err){ 
      res.json(err); 
      console.log(group); 
     } 
     if(group){ 
      //If group is found loop trough the users 
      for(var i = o; i < group.users.lenght; i++){ 
       //Check if the current user is somebody else as the author 
       if(group.users[i]._id != req.user._id){ 
        //Push and save the new post to every user in the group 
        //We push the _id of the post because we are using references 
        group.users[i].unread.push(newPost._id); 
        group.users[i].save(function(err){ 
         if(err){ 
          throw err; 
         } 
        }); 
       } 
      } 
     }else{ 
      //Do what you need to do if group hasn't been found 
     } 
    }); 
} 

此代码假定您req.user字段中填入为用户模式。

此代码未经测试。所以请调试它。 :)

+0

非常感谢,@Snekw。我会在稍后检查))您认为,有没有更好的方法来检查用户是否查看了帖子?我认为我的方式不是最好的。 – Ramil

+0

我可以想到两种不同的方式。存储关于谁查看过的信息以及其他信息是存储帖子上的信息。两者都有其优点。如果你不关心如何轻易地了解每个查看过该文章的用户,那么将其存储在用户上会更好。如果您想知道所有查看过的用户都可以轻松发帖,那么我会将信息存储在帖子中并从那里检索。您可以将它们组合起来,并将信息存储在这两者上并从那里开始工作。 – Snekw