简体   繁体   中英

Visual studio code catching syntax errors

I am using Visual studio code for coding 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 .

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 . Can someone kindly guide how i can get proper Intellisense and error handling for Node js code in VS code.

You might want to use Typescript instead of vanilla Javascript. Its pretty much the same in syntax, but its a typed version of Javascript. Apart from that, a linter is also useful for this kind of things. For typescript you would use TSlint, but you can use ESlint for pure javascript. Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM