2016-04-15 121 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