无法将Meteor集合中的数据放入数组中(Can't put data from a Meteor collection into an array)
我正在学习Meteor,我试图将Collection.find()的结果传递给数组(使用变量),而我所拥有的更简单的代码(在根目录中的文件中):
CalEvents = new Mongo.Collection('calevents'); //creating a collection /*------------------------- Populating the database with dummy data-------*/ if (Meteor.isServer) { Meteor.startup(function () { if (CalEvents.find().count() === 0) { CalEvents.insert({ title: "Initial room", start: '2010-02-02' }); } }); } /*--------------- Creating an array from the collection-----------------*/ events = []; calEvents = CalEvents.find({}); calEvents.forEach(function(evt){ events.push({ title: evt.title, start: evt.start, }) });
该页面没有任何显示,但使用我可以看到的控制台(CalEvents.find(。。fetch()),我在我的数据库中有数据,但“事件”变量是空的...我不明白为什么因为我尝试了其他一些事情,比如更改文件名和移动代码以保证正确的顺序。 我已经尝试使用CalEvents.find()。fetch()来创建一个数组,将结果放入变量但是我无法做到......有谁知道什么是如此简单以至于我错过了? ...
I'm learning Meteor and I was trying to pass the result of a Collection.find() into and array (using a variable) and the simpler code I have is (in a file that is in the root):
CalEvents = new Mongo.Collection('calevents'); //creating a collection /*------------------------- Populating the database with dummy data-------*/ if (Meteor.isServer) { Meteor.startup(function () { if (CalEvents.find().count() === 0) { CalEvents.insert({ title: "Initial room", start: '2010-02-02' }); } }); } /*--------------- Creating an array from the collection-----------------*/ events = []; calEvents = CalEvents.find({}); calEvents.forEach(function(evt){ events.push({ title: evt.title, start: evt.start, }) });
The page has nothing to show but using the console I can see (CalEvents.find().fetch()) that I have data in my database but the "events" variable is empty... I can't understand why because I tried several other things such as changing file names and moving code to guarantee the proper order. And I already tried to use CalEvents.find().fetch() to create an array an put the result into a variable but I'm not able to do it... Does anyone know what's so simple that I'm missing?...
原文:https://stackoverflow.com/questions/28353240
最满意答案
这 (来自regexplib.com )将匹配你想要的,并执行闰年,每月每天等的检查。它比你想要的更容忍分隔符,但这可以很容易地修复。 如你所见,它相当可怕。
或者(最好在我看来)你可能只想检查正确位置的数字,然后在代码中执行闰年和每月每日检查。 有时一个正则表达式不是那么容易理解,并且明确地在代码中执行检查更清晰(因为您可以准确地报告错误 - “11月仅30天”,而不是“不匹配模式”消息,这是旁边无用)
This (from regexplib.com) will match what you want, and perform checks for leap years, days-per-month etc. It's a little more tolerant of separators than you want, but that can be easily fixed. As you can see, it's rather hideous.
Alternatively (and preferably in my opinion) you may want to simply check for figures in the correct places, and then perform leap year and days-per-month checks in code. Sometimes one regexp isn't so understandable and there's greater clarity in performing the checks in code explicitly (since you can report precisely what's wrong - "only 30 days in November", rather than a "doesn't match pattern" message, which is next to useless)
相关问答
更多-
您的问题的答案是,允许反向引用的“正则表达式”语言既不规则也不上下文。 (换句话说,正如你所指出的那样,你不能用普通语言和CFL来模拟反向引用。)事实上,维基百科说,我们在实践中使用的许多“正则表达式”语言都是NP-Complete : 在许多现代工具的支持下,与无限数量的后向引用进行模式匹配是NP完全的(参见定理6.2)。 正如其他人所建议的那样,计算机语言和库中普遍支持的正则表达式语言与正式语言理论中的正则表达式不同。 Larry Wall在Perl中写到 “正则表达式”, “正则表达式”与真正的正则表 ...
-
您正在使用的Boost库提供了一种将重复的组捕获到堆栈中的方法, 前提是您使用BOOST_REGEX_MATCH_EXTRA标志集编译库,否则将没有名为captures的成员。 当你使用boost::regex_search或boost::regex_match ,传递boost::match_extra标志,你将使用你的(.)* (匹配并捕获任何字符但是换行符,零次或多次出现)捕获所有值(.)*通过sub_match对象的capture成员可访问的堆栈。 以下是官方Boost网站的演示方法: #inclu ...
-
可以使用正则表达式来匹配嵌套模式吗?(Can regular expressions be used to match nested patterns? [duplicate])[2021-12-28]
不,这很简单。 有限自动机(它是正则表达式的基础的数据结构)除了它的状态之外没有内存,如果你有任意深度的嵌套,则需要一个任意大的自动机,这与自动机的概念相冲突。 您可以将嵌套/配对元素匹配到一个固定的深度,其深度仅受您的内存限制,因为自动机变得非常大。 但实际上,您应该使用下推自动机,即无上下文语法的解析器,例如LL(自上而下)或LR(自下而上))。 您必须考虑更差的运行时行为:O(n ^ 3)与O(n),n =长度(输入)。 有许多解析器生成器可以使用,例如Java的ANTLR 。 找到Java(或C)的 ... -
使用正则表达式(Using regular expressions)[2022-12-03]
如果你匹配HTML,你可能会匹配a标签中的href和a标签的内容。 uk@example1.com if you are matching HTML you are probably matching both the href in the a tag and the content of the a tag. uk@example1.com -
如何在python中使用正则表达式匹配特定的时间和日期(How to match the specific time and date using regular expressions in python)[2022-10-28]
我会选择一个略有不同的正则表达式: ^Sep 12 09:23:(?:4[5-9]|50) 说明: [45-50]是一个匹配4的字符类, 5到5之间的所有字符和0 。 这是因为字符类是char-by-char。 对此的经典修复是通过数字前缀定义替代方案: (?:...)是一个用于节省一些资源的非记录组 4[5-9]匹配数字45 ,... 49 另一种选择是50 ,你的间隔的上限。 在这里演示。 您可以确保只编译一次正则表达式。 所以你的脚本使用更少的内存和CPU: import fileinput,re ... -
如果您传递给它的整个字符串是一个日期,您的RegEx似乎只会找到匹配项。 尝试删除^和$ 以下是使用RegEx重新编写的示例,它将以mm / dd / yyyy和mm-dd-yyyy格式查找日期 - Private Sub TestDate() MsgBox RegExDate("cancel on 12/21/2010 ") End Sub Private Function RegExDate(s As String) As String Dim re, match Set re ...
-
这 (来自regexplib.com )将匹配你想要的,并执行闰年,每月每天等的检查。它比你想要的更容忍分隔符,但这可以很容易地修复。 如你所见,它相当可怕。 或者(最好在我看来)你可能只想检查正确位置的数字,然后在代码中执行闰年和每月每日检查。 有时一个正则表达式不是那么容易理解,并且明确地在代码中执行检查更清晰(因为您可以准确地报告错误 - “11月仅30天”,而不是“不匹配模式”消息,这是旁边无用) This (from regexplib.com) will match what you want, ...
-
将字符串解析为DateTime并比较它们会不会更容易? Wouldn't it be easier to parse the strings to DateTimes and comparing those?
-
Java中的正则表达式:匹配由其他数据包围的日期值(Regular Expressions in Java: Matching a date value surrounded by other data)[2023-02-27]
^符号匹配字符串的开头, $匹配结束。 删除它们允许模式匹配字符串中的日期。 喜欢这个: "(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d(?:,)" The ^ sign matches the start of the string and $ matches the end. Removing those allows the pattern to match dates within the string. Like ... -
正则表达式匹配(Regular Expressions nth match)[2022-08-04]
$result = preg_replace('/(?:[^,]*,){4}/', '\0\r\n', $subject); 这匹配一行中的四个逗号分隔值(我假设您不会在组内的字符串中使用逗号)并在它们之后添加一个CRLF。 [编辑]以上是基于PHP的解决方案 对于纯MySQL解决方案,请安装lib_mysqludf_preg并使用: SELECT preg_replace('/(?:[^,]*,){4}/', '${0}\r\n', `fieldname`) as 'new_layout' fr ...