检查方法返回未定义

Check method is returning undefined

本文关键字:未定义 返回 方法 检查      更新时间:2023-09-26

我已经阅读了文档。 并研究一些check package教程。 流星文档,尝试再次安装检查包 大气JS,并查看它易于使用。但

每次我尝试这样的事情时:

check("this is string",String); //return undefined
var objectTest = { some : 0 };
check(objectTest, Object); //undefined
check(objectTest.some, Number); //undefined

它总是返回未定义,我不知道为什么,如果我在 chrome 控制台上写下检查返回,check函数就在那里

function (value, pattern) {                                              // 19
  // Record that check got called, if somebody cared.                            // 20
  //                                                                             // 21
  // We use getOrNullIfOutsideFiber so that it's OK to call check()              // 22
  // from non-Fiber server contexts; the downside is that if you forget to       // 23
  // bindEnvironment on some random callback in your method/publisher,           // 24
  // it might not find the argumentChecker and you'll get an error about         // 25
  // not checking an argument that it looks like you're checking (instead        // 26
  // of just getting a "Node code must run in a Fiber" error).                   // 27
  var argChecker = currentArgumentChecker.getOrNullIfOutsideFiber();             // 28
  if (argChecker)                                                                // 29
    argChecker.checking(value);                                                  // 30
  try {                                                                          // 31
    checkSubtree(value, pattern);                                                // 32
  } catch (err) {                                                                // 33
    if ((err instanceof Match.Error) && err.path)                                // 34
      err.message += " in field " + err.path;                                    // 35
    throw err;                                                                   // 36
  }                                                                              // 37
}

所以功能在那里,但我不知道如何使用它......你知道为什么它返回未定义吗?(顺便说一句,我谷歌"流星检查方法返回未定义"没有成功)

感谢您的支持。

如果出于某种原因,我使用铁路线我没有使用Metero.publishMeteor.methods内部的功能

check

返回任何内容。这就是为什么它似乎总是未定义的。

如果值与模式不匹配,则会引发异常。如果值与模式匹配,则不匹配。

因此,与其使用 if (check(...)) { ,不如编写如下代码:

function sample(name) {
    check(name, String);
    // code down here won't run if name is not a string
    return name.toUpperCase();
}