流星 - MongoDB _id

时间:2016-09-08 23:17:57

标签: mongodb meteor

我想将MongoDB _id主键分配给变量,但不能这样做。当我查看集合中的记录时,postID显示为undefined - 下面列出的代码。我怎样才能解决这个问题?我希望postID_id匹配。提前谢谢!

Template.postNewJob.events({
    'submit form': function(event) {
    event.preventDefault();
    var position = $('[name=position]').val();
    var jobDescription = $('[name=jobDescription]').val();
    var createdAt = new Date();
    var createdBy = Meteor.userId();
    var postID = this._id;
    postedJobs.insert({
        position: position,
        jobDescription: jobDescription,
        createdAt: createdAt,
        createdBy: createdBy,
        postID: postID
    });
    Router.go('dashboard');
    }
});

2 个答案:

答案 0 :(得分:0)

如果postId永远是您的文档ID,那么可能还有另一种方法。您可以使用收集钩子。请参考这里 https://github.com/matb33/meteor-collection-hooks

并在服务器端文件中使用以下功能

postedJobs.after.insert(function (userId, doc) {
  doc.postId=this._id
});

答案 1 :(得分:0)

我能够从其他帖子中找到一些有用的信息,并且能够解决此问题。参考页面为objectId from url to match with one in the mongo using meteorjs

谢谢!