简体   繁体   English

Visual Studio 代码捕获语法错误

[英]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.

相关问题 Visual Studio Code 在 javascript 文件中显示错误,javascript 没有语法错误(据我所知) - Visual Studio Code showing errors all over javascript files, javascript has no syntax errors (to my knowledge) 使用then / catch语法通过axios分别捕获4xx和代码错误 - Catching 4xx and code errors separately with axios using then / catch syntax Visual Studio Razor页面错误语法错误 - Visual Studio Razor Page False Syntax Errors 在 Visual Studio Code 中使用装饰器时出错 - Errors when using decorators in Visual Studio Code Visual Studio 2015编辑器使用ECMA Script 6语法显示错误 - Visual studio 2015 editor display errors with ECMA Script 6 syntax 在Visual Studio中查找大型JavaScript文件中的语法错误 - Finding syntax errors in large javascript file in Visual Studio 将 TypeScript 与 Jest 一起使用不会捕获语法错误 - Using TypeScript with Jest not catching syntax errors 在Aptana Studio 3中禁用JavaScript文件中PHP代码的语法错误 - Disable syntax errors for PHP code inside JavaScript file in Aptana Studio 3 Visual Studio Code 语法突出显示不适用于 JavaScript 和 TypeScript - Visual Studio Code syntax highlighting is not working for JavaScript and TypeScript JSHint 无法识别 Visual Studio Code (VSCode) 中的 Async/Await 语法 - JSHint does not recognise Async/Await syntax in Visual Studio Code (VSCode)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM