1 | 'use strict'; |
||
2 | |||
3 | const {MongoClient} = require('mongodb'); |
||
4 | |||
5 | MongoClient.connect('mongodb://localhost:27017/JokesApp', (err, db) => { |
||
6 | if (err) |
||
7 | return console.log('unable to connect MongoDB' + err); |
||
0 ignored issues
–
show
Debugging Code
introduced
by
![]() Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
8 | console.log('Connected to MongoDB Server'); |
||
9 | |||
10 | // creating a new collection, a table like structure |
||
11 | db.collection('Jokes').insertOne({ |
||
12 | joke : 'Sample joke 2', |
||
13 | likes : 0, |
||
14 | }, (err, result) => { |
||
15 | if (err) |
||
16 | return console.log('unable to insert data' + err); |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
17 | |||
18 | console.log(JSON.stringify(result.ops, undefined, 2)); |
||
0 ignored issues
–
show
|
|||
19 | }); |
||
20 | |||
21 | // one more users collection |
||
22 | db.collection('Users').insertOne({ |
||
23 | name : 'Ashok Dey', |
||
24 | age : 22, |
||
25 | location : 'Delhi' |
||
26 | }, (err, result) => { |
||
27 | if (err) |
||
28 | return console.log('Failed to insert data into the user collection'); |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
29 | |||
30 | console.log(JSON.stringify(result.ops, undefined, 2)); |
||
0 ignored issues
–
show
|
|||
31 | }); |
||
32 | |||
33 | // close the database |
||
34 | db.close(); |
||
0 ignored issues
–
show
|
|||
35 | }); |