我已阅读文档。并研究一些检查包
教程。 meteor docs ,尝试再次安装检查包atmospherejs ,并且看起来使用起来很简单。但是
每次我尝试这样的事情:
check("this is string",String); //return undefined
var objectTest = { some : 0 };
check(objectTest, Object); //undefined
check(objectTest.some, Number); //undefined
它总是返回未定义,我不知道为什么,如果我在 chrome 控制台上写 check return ,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
}
所以这个函数就在那里,但我不知道如何使用它......你知道为什么它返回未定义吗? (顺便说一句,我谷歌“ meteor 检查方法返回未定义”没有成功)
感谢您的支持。
//如果出于某种原因,我正在使用铁路线
//我没有使用 Metero.publish
或 Meteor.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();
}
关于javascript - 检查方法返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27139975/