MongoDB & NodeJS: the options [servers] is not supported && the options [caseTranslate] is not supported

Viewed 2095

Here, I am currently learning how to use MongoDB with NodeJS, but here is 1h or 2h blocked on internet looking now for a solution with 2 errors that I have and impossible to find the problem here is my code (Sorry, I am French, in the console.log, the sentences are in fr):

var MongoClient = require('mongodb').MongoClient

const mongodbUrl = "mongodb://localhost:27017/"
let client = new MongoClient(mongodbUrl, {useNewUrlParser: true})
let essai
var randomstring = '';

test()

function test(){

    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz-*/+.,?;:!§ù%$*£¤¨^=+})°]à@ç^_\è`-|(['{#é~&²";
    var string_length = 14;
    randomstring = '';

        for (var i=0; i<string_length; i++) {

            var rnum = Math.floor(Math.random() * chars.length);
            randomstring += chars.substring(rnum,rnum+1);

        }
        
    //console.log(randomstring)
    CheckBDD(randomstring)
    //setTimeout(1000)

    }

        

function CheckBDD(randomstring){

    client.connect((err, client) => {
    
        if (err) throw err
    
        //console.log("Connexion à la bdd avec succès")
        const dbName = "wordlist"
        let db = client.db("wordlist")
        var collection = 'wordlist';
    

    db.collection('wordlist').findOne({wordlist: randomstring}, (err, essai) => {
        if (err) throw err
        //console.log(essai)

        if (essai == null)
        {

            db.collection('wordlist').insertOne( { wordlist: randomstring} );
            console.log("Valeur inconnue:" + randomstring)

        } else {

            console.log("Valeur déjà connue:" + randomstring)

        }

        test()

    });
    
});


}

Errors:

the options [servers] is not supported

the options [caseTranslate] is not supported

From what I found, it will come from the connection options but... There you go. I hope you can help me thank you very much in advance,

Owzlaa

1 Answers

I just started having the exact same "errors" appearing in some of my database requests. It isn't all of the time though. I had exactly the same call in two different places in my code: one inside my server's connection callback and another in a module used inside of a route controller. The one inside my server's connection callback did not trigger the error whereas the other one did.

Doing a console log on the client object showed exactly the same options. So what was the difference? The server connection callback was only a "read operation" whereas the one in the other module eventually calls a method to perform an update on the collection itself.

I modified the code to have a conditional in it so when one case is true it modifies the collection and when the other case is true it does not. Guess what? The second case, where there's no modification to the collection, does not trigger the error. It would seem that doing some sort of write causes this thing to trigger.

I am not sure I want to call it an error. It's more like a warning because it is non-blocking (at least in my case). It also doesn't seem to trigger any of my error handling.

I still don't have a solution for making it go away other than perhaps we have to make copies of the database object... I haven't tried it yet though.

// Express code : NOTE - client is the MongoClient exported from somewhere else
      let db;
      client.connect()
            .then(c => {
                if (c) {
                    db = c.db(DATABASE_NAME);

                    req.client = c;

                    return verifiers.verifyForFiles(db);
                } else {
                    req.errStatus = 500;
                    throw new Error('Database error');
                }
            })
            .then(checkResult => {
                const { hasCollection, hasSchema } = checkResult;

                if (!hasCollection) {
                    /** triggers errors */
                    return collections.createFilesCollection(db);
                } else {
                    if (hasSchema) {
                        /** does not trigger errors */
                        return;
                    } else {
                        /** triggers errors */
                        return collections.updateFilesCollection(db);
                    }
                }
            })
            .then(() => next())
            .catch(next);
// more Express stuff
Related