Meteor调试和显示警报、日志和消息

3
我刚接触Meteor,仍在努力理解一些东西。为了更好地理解该语言和工作流程,使用警报和将消息打印到控制台是一个好主意。但我遇到了几个问题。 我想知道是否有一种简单和默认的方法将消息打印到控制台。例如,当我提交表单时,我不能使警报起作用,并且我看不到我打印的浏览器控制台消息。 我该怎么做?也许是将消息打印到运行服务器的系统控制台上?如何使用控制台来显示消息和警报?

1
如果您正在使用Chrome进行表单提交,如果想要控制台日志记录,可以使用“保留日志复选框”选项来保留日志。 - Sasikanth
很棒的答案。这真的很有帮助。但是你知道如何在系统控制台上显示日志吗? - Victor Ferreira
不,我不知道其他的方法。我也在寻找这个问题的好答案。 - Sasikanth
我遇到了一个问题。我设置了一个回调函数到 Collection.insert 方法。如果我尝试使用 console.log('Test'),它不会打印任何内容。但是如果我用返回的 ID 打印 console.log(id),它会在控制台中打印出ID。如果我使用变量而不是字符串也失败了。似乎我们不能依赖这个方法?发生了什么? - Victor Ferreira
1个回答

1
你基本上会使用console.log()来打印javascript控制台中的日志,这基本上是此函数的作用。如果您在期望时没有在Chrome控制台中看到任何结果,则可能有以下几个原因:
  • 执行代码时未到达console.log()。确保它不在错误的条件范围内。

例如:

if (false)
  console.log('print me!'); // this desperate log will never see the light of day
else
  console.log('No, print me!'); // this one always will
  • 您的console.log()在服务器端运行,因此其输出将打印在您的服务器日志中,通常是在运行Meteor的侧面终端窗口中。

示例:

if (Meteor.isServer)
  console.log('I\'m on the server!'); // this log will print on the server console
if (Meteor.isClient)
  console.log('I\'m on the client!'); // this log will print on the chrome console
console.log('I\'m omnipresent'); // this log will print on both
  • 如果出现了提到undefined的错误弹出,那么您尝试打印的变量尚未定义。确保在打印之前设置好您的变量。

例子:

> var real_friend = "Bobby";
> console.log(real_friend);
"Bobby"
> console.log(imaginary_friend);
Error: 'imaginary_friend' is undefined.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接