简体   繁体   English

如何在新的Meteor Collection文档中在服务器上设置created_on字段?

[英]How do I set a created_on field on the server in a new Meteor Collection document?

I'm building a chat room that displays posts in chronological order. 我正在建立一个按时间顺序显示帖子的聊天室。 I'm currently setting the time of the user's post in the client js on the submit event of the "entry" template: 我目前正在“条目”模板的提交事件中设置客户端js中用户帖子的时间:

Template.entry.events =  {
  'submit': function(e){
    e.preventDefault();
    //console.log(this.userId());
    var user = Meteor.user();
    var roomName = Session.get('currentRoomName');
    Messages.insert({
      user: user,
      room_name: roomName,
      message: $('#message').val(),
      created_on: new Date().getTime()
    });
    $("#message").val('');
  }
};

My issue is that because this date is set on the client, if a user changes the time on their machine, it will be recorded as such allowing the user to post ahead of or behind the current conversation. 我的问题是因为这个日期是在客户端上设置的,如果用户在他们的机器上更改时间,它将被记录为允许用户在当前对话之前或之后发布。 Ideally, I'd be able to update the created_on time on the server after receiving a new chat entry to reflect the current UTC time on the server and correct whatever value comes in. I'd still want to have the time on the client for latency compensation, etc. 理想情况下,我可以在收到新的聊天条目后更新服务器上的created_on时间,以反映服务器上的当前UTC时间并更正任何值。我仍然希望在客户端上有时间延迟补偿等

I've read through a lot of the Meteor documentation on II have a suspicions that I might be solve this using .observe(), but I'm new to both Meteor and Mongodb. 我已经阅读了很多关于II的Meteor文档,怀疑我可能会使用.observe()来解决这个问题,但我是Meteor和Mongodb的新手。 Would it be crazy to observe every document in the entry collection? 观察入门集合中的每个文档会不会很疯狂? Is it possible to only observe new entries? 是否可以仅观察新条目?

I'm also using the auth branch - not sure if that will make any difference. 我也在使用auth分支 - 不确定这是否会有所不同。

If there were a lot of users in a chat room, this non-question might end up being related. 如果聊天室中有很多用户, 这个非问题可能最终会被关联。

Apologies if this is something super easy in Mongo/Meteor that I'm just missing. 如果在Mongo / Meteor中这是一件非常容易的东西,我只是遗憾地道歉。

The easiest solution is to use a method rather than the implicit one created by Messages.insert . 最简单的解决方案是使用方法而不是Messages.insert创建的隐式方法。

On the server: 在服务器上:

Meteor.methods({
  addMessage: function(room_name, message) {
    Messages.insert({
      userId: this.userId,
      room_name: room_name,
      message: message,
      created_on: new Date().getTime();
    });
  });
});

On the client: 在客户端:

Template.entry.events =  {
  'submit': function(e) {
    e.preventDefault();
    Meteor.call('addMessage', Session.get('currentRoomName'), $('#message').val());
  }
}

This will create a record immediately on the client (with the client's date), simultaneously creating a record on the server (with the server's date), and it'll then overwrite the client's record when it next syncs. 这将立即在客户端创建记录(使用客户端的日期),同时在服务器上创建记录(使用服务器的日期),然后在下次同步时覆盖客户端的记录。

I strongly suggest you the ACL introduced in auth branch. 我强烈建议您在auth分支中引入ACL。 You can do some validations and/or modifications when insert/update/remove/fetch. 插入/更新/删除/获取时,您可以进行一些验证和/或修改。 Take a look at the Meteor.Collection.prototype.allow under the source file packages/mongo-livedata/collection.js . 看看在Meteor.Collection.prototype.allow源文件下packages/mongo-livedata/collection.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Meteor:如何将用户集合中的用户名添加到我创建的新集合中? - Meteor: How to add Username from Users collection to a new collection I created? 如何使用Meteor通过ID从另一个集合中的文档返回文档属性? - How do I return document property from document in another collection by ID using Meteor? Meteor DDP:如何在将新文档添加到集合时收到通知 - Meteor DDP: How to get notified when a NEW document is added to a Collection Meteor JS:如何将文档插入集合中,但仅限于客户端? - Meteor JS: How do I insert a document into a collection, but only on the client-side? 如何将集合插入流星的表单中 - how do I insert a collection into a form in meteor 如何使用一个集合中的文档字段从不同集合中检索另一个文档字段? - How do I use a document's field from one collection to retrieve another document field from a different collection? 我收到 Postgresql 的 created_on 日期的语法错误 - I'm getting a syntax error for the date created_on for Postgresql 流星:如何保存到集合并从其中获取数据? - Meteor: How do I save to a collection and getdata out of it? 我如何实现“更多加载”按钮来收集流星 - how do i implement a “load more” button for meteor collection 如何从Meteor中的列表集合中删除项目? - How do I remove an item from a lists collection in Meteor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM