[英]Visual studio code catching syntax errors
I am using Visual studio code for coding Nodejs.我正在使用 Visual Studio 代码来编码 Nodejs。 I very strange and annoying that almost everything I write passes until runtime it fails.我很奇怪也很烦人,我写的几乎所有东西都经过,直到运行时它失败。
For example, I wrote this code例如,我写了这段代码
function validateRentalReturn(rental) {
const schema = Joi.object({
customerId: Joi.objectId().required,
movieId: Joi.objectId().required
});
return schema.validate(rental);
}
and I forgot to add ()
at the end of required()
and it didn't complain or anything until at run time I called this method and I started getting the following error "movieId" failed custom validation because Cannot read property 'presence' of undefined
.我忘了在required()
的末尾添加()
并且它没有抱怨或任何东西,直到在运行时我调用此方法并开始收到以下错误"movieId" failed custom validation because Cannot read property 'presence' of undefined
。
I had no clue about this error but finally got it sorted, in the intellisense it shows like following我对这个错误一无所知,但最终得到了排序,在智能感知中它显示如下
Now how should i know that required
is a property
or a function
.现在我怎么知道required
是一个property
还是一个function
。 Can someone kindly guide how i can get proper Intellisense and error handling for Node js code in VS code.有人可以指导我如何在 VS 代码中为 Node js 代码获得正确的智能感知和错误处理。
You might want to use Typescript instead of vanilla Javascript.您可能想要使用 Typescript 而不是 vanilla Javascript。 Its pretty much the same in syntax, but its a typed version of Javascript.它的语法几乎相同,但它是 Javascript 的类型化版本。 Apart from that, a linter is also useful for this kind of things.除此之外,linter 对这类事情也很有用。 For typescript you would use TSlint, but you can use ESlint for pure javascript.对于打字稿,您将使用 TSlint,但您可以将 ESlint 用于纯 javascript。 Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.