Issues (82)

app/experiments/mongo-fetch.js (11 issues)

1
// import MongoClient from the mongodb package
2
const {MongoClient} = require('mongodb');
3
4
// connect to mongodb server
5
MongoClient.connect('mongodb://localhost:27017/JokesApp', (err, db) => {
6
    if (err) {
7
        return console.log('Failed to connect to mongoDB server : ' + err);
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
8
    }
9
10
    console.log('Connected to MongoDB Server');
11
12
    //get the count of jokes stored
13
    db.collection('Jokes').count().then((count) => {
14
        console.log(`Jokes count = ${count}\n`);
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
15
    }, (err) => {
16
        if (err) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if err is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
17
            return console.log('error getting count', err);
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
18
        }
19
    });
20
21
    // fetch all the jokes/documets with 10 likes
22
    db.collection('Jokes').find({likes : 10}).toArray().then((docs) => {
23
        console.log(JSON.stringify(docs, undefined, 2));
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
24
    }, (err)=> {
25
        if (err) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if err is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
26
            return console.log('Fetch error : ', err);
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
27
        }
28
    });
29
30
    //fetch all the users from the db
31
    db.collection('Users').find().toArray().then((docs) => {
32
        // print all the users 
33
        console.log(JSON.stringify(docs, undefined, 2));
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
34
    }, (err) => {
35
        if(err) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if err is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
36
            return console.log('Error Fetching users : ', err);
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
37
        }
38
    });
0 ignored issues
show
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
39
40
    //db.close();
41
});