使用变量创建动态流行图集 [英] Create a dynamic meteor collection using a variable

查看:138
本文介绍了使用变量创建动态流行图集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用变量创建一个动态流星集合,因此每次提交表单并执行事件时,都会创建一个新的流星集合。看看我正在寻找的代码,虽然不行。
(请记住,我还处于早期的生产阶段,所以我没有设置特定的服务器端或客户端进行调试,同时,忽略任何语法或结构错误,因为我只是键入这个。工作)

I am trying to create a dynamic meteor collection using a variable so a new meteor collection will be created everytime an form is submitted and an event is executed. See code below for what I am looking for though is does not work. (Keep in mind I am still in the early production stages so I have not set up specific server or client side for debugging purposes. Also, disregard any grammatical or structure errors as i just typed this. just how to make it work)

预期结果

假设用户1流星ID为x533hf4j3i

Suppose user 1 meteor id is x533hf4j3i

假设用户2流星ID是jf83jfu39d

Suppose user 2 meteor id is jf83jfu39d

OUTCOME:x533hf4j3ijf83jfu39d = new Mongo.Collection('x533hf4j3ijf83jfu39dmessages')

OUTCOME: x533hf4j3ijf83jfu39d = new Mongo.Collection('x533hf4j3ijf83jfu39dmessages')

此示例代码不工作

Template.createChat.events({
  'submit form': function(event){
    event.preventDefault();
    var messageRecipientVar = event.target.messageRecipient.value;
    var currentUserId = Meteor.userId();
    var recipientUserId = Meteor.users.findOne(messageRecipientVar)._id;
    var chatCollectionNameVar = {$concat: [currentUserId, recipientUserId]}
    var chatCollectionName = {$concat: [currentUserId, recipientUserId, "messages"]}

    chatCollectionNameVar = new Mongo.Collection('chatCollectionName');
  }

});


推荐答案

不要这样做要求如何创建动态集合定期与新的流星开发人员一起出现,但从来不是正确的方法。 @ david-wheldon很好地描述了为什么不在此页面的底部进行此操作。

Don't do this. Asking how to create dynamic collections comes up periodically with new meteor developers, but it's never the right approach. @david-wheldon has a great description of why not to do this at the bottom of this page.

只需使用一个集合消息,其中包含以下文件:

Just use one collection Messages, containing documents something like this:

{ _id: xxxxxx,
  sender: 'x533hf4j3i',
  recipient:  'jf83jfu39d',
  message:    'Hi there!',
  ...
  timestamp, etc
  ...
}

那么这取决于你的应用程序,如果一个用户可以看到他们没有发送/接收的消息,如果你需要过滤这个,你可以在发布功能的服务器端。

Then it depends on your app if a user can view messages they did not send/receive, and if you need filtering on this you would do it server side in a publish function.

无论哪种方式,如果您只想要两个用户之间的消息,您可以这样查询:

Either way, on the Client if you just want the messages between two users you would query like this:

chatMessages = Messages.find(
    {$or: [{ sender: 'x533hf4j3i', recipient:  'jf83jfu39d'}, 
           { sender: 'jf83jfu39d', recipient:  'x533hf4j3i'}
     ]}).fetch()

这篇关于使用变量创建动态流行图集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆