Getting Error This.options = options ? {};

I am getting the same error and my node is also v20.8.0

I am getting the same error and my node is also v20.8.0

Hey @Harsh_Prajapati / @Akash_Kumar9,

Could you please let me know if the problem still persists for you? If it does, please share the Node.js driver/Mongoose version you are using and the error log you are encountering.

Regards,
Kushagra

Hello! I’m having this error too :frowning:

How I’m trying running:

monicaveloso@SJC-UBU-001459:~/about/FruitsProject$ node -v
v21.0.0
monicaveloso@SJC-UBU-001459:~/about/FruitsProject$ node app.js
(node:1436578) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
null
monicaveloso@SJC-UBU-001459:~/about/FruitsProject$ nodejs app.js
/home/monicaveloso/about/FruitsProject/node_modules/mongodb/lib/operations/add_user.js:16
        this.options = options ?? {};
                                ^

SyntaxError: Unexpected token ?
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/home/monicaveloso/about/FruitsProject/node_modules/mongodb/lib/admin.js:4:20)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)

My package_lock.json

  "name": "fruitsproject",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "fruitsproject",
      "version": "1.0.0",
      "license": "ISC",
      "dependencies": {
        "mongodb": "^5.9.0"
      }
    },
    "node_modules/@mongodb-js/saslprep": {
      "version": "1.1.0",
      "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.0.tgz",
      "integrity": "sha512-Xfijy7HvfzzqiOAhAepF4SGN5e9leLkMvg/OPOF97XemjfVCYN/oWa75wnkc6mltMSTwY+XlbhWgUOJmkFspSw==",
      "optional": true,
      "dependencies": {
        "sparse-bitfield": "^3.0.3"
      }
    },
    "node_modules/@types/node": {
      "version": "20.8.7",
      "resolved": "https://registry.npmjs.org/@types/node/-/node-20.8.7.tgz",
      "integrity": "sha512-21TKHHh3eUHIi2MloeptJWALuCu5H7HQTdTrWIFReA8ad+aggoX+lRes3ex7/FtpC+sVUpFMQ+QTfYr74mruiQ==",
      "dependencies": {
        "undici-types": "~5.25.1"
      }
    },
    "node_modules/@types/webidl-conversions": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.2.tgz",
      "integrity": "sha512-uNv6b/uGRLlCVmelat2rA8bcVd3k/42mV2EmjhPh6JLkd35T5bgwR/t6xy7a9MWhd9sixIeBUzhBenvk3NO+DQ=="
    },
    "node_modules/@types/whatwg-url": {
      "version": "8.2.2",
      "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-8.2.2.tgz",
      "integrity": "sha512-FtQu10RWgn3D9U4aazdwIE2yzphmTJREDqNdODHrbrZmmMqI0vMheC/6NE/J1Yveaj8H+ela+YwWTjq5PGmuhA==",
      "dependencies": {
        "@types/node": "*",
        "@types/webidl-conversions": "*"
      }
    },
    "node_modules/bson": {
      "version": "5.5.1",
      "resolved": "https://registry.npmjs.org/bson/-/bson-5.5.1.tgz",
      "integrity": "sha512-ix0EwukN2EpC0SRWIj/7B5+A6uQMQy6KMREI9qQqvgpkV2frH63T0UDVd1SYedL6dNCmDBYB3QtXi4ISk9YT+g==",
      "engines": {
        "node": ">=14.20.1"
      }
    },
    "node_modules/ip": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.0.tgz",
      "integrity": "sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ=="
    },
    "node_modules/memory-pager": {
      "version": "1.5.0",
      "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz",
      "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==",
      "optional": true
    },
    "node_modules/mongodb": {
      "version": "5.9.0",
      "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.9.0.tgz",
      "integrity": "sha512-g+GCMHN1CoRUA+wb1Agv0TI4YTSiWr42B5ulkiAfLLHitGK1R+PkSAf3Lr5rPZwi/3F04LiaZEW0Kxro9Fi2TA==",
      "dependencies": {
        "bson": "^5.5.0",
        "mongodb-connection-string-url": "^2.6.0",
        "socks": "^2.7.1"
      },
      "engines": {
        "node": ">=14.20.1"
      },
      "optionalDependencies": {
        "@mongodb-js/saslprep": "^1.1.0"
      },
      "peerDependencies": {
        "@aws-sdk/credential-providers": "^3.188.0",
        "@mongodb-js/zstd": "^1.0.0",
        "kerberos": "^1.0.0 || ^2.0.0",
        "mongodb-client-encryption": ">=2.3.0 <3",
        "snappy": "^7.2.2"
      },
      "peerDependenciesMeta": {
        "@aws-sdk/credential-providers": {
          "optional": true
        },
        "@mongodb-js/zstd": {
          "optional": true
        },
        "kerberos": {
          "optional": true
        },
        "mongodb-client-encryption": {
          "optional": true
        },
        "snappy": {
          "optional": true
        }
      }
    },
    "node_modules/mongodb-connection-string-url": {
      "version": "2.6.0",
      "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-2.6.0.tgz",
      "integrity": "sha512-WvTZlI9ab0QYtTYnuMLgobULWhokRjtC7db9LtcVfJ+Hsnyr5eo6ZtNAt3Ly24XZScGMelOcGtm7lSn0332tPQ==",
      "dependencies": {
        "@types/whatwg-url": "^8.2.1",
        "whatwg-url": "^11.0.0"
      }
    },
    "node_modules/punycode": {
      "version": "2.3.0",
      "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz",
      "integrity": "sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA==",
      "engines": {
        "node": ">=6"
      }
    },
    "node_modules/smart-buffer": {
      "version": "4.2.0",
      "resolved": "https://registry.npmjs.org/smart-buffer/-/smart-buffer-4.2.0.tgz",
      "integrity": "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==",
      "engines": {
        "node": ">= 6.0.0",
        "npm": ">= 3.0.0"
      }
    },
    "node_modules/socks": {
      "version": "2.7.1",
      "resolved": "https://registry.npmjs.org/socks/-/socks-2.7.1.tgz",
      "integrity": "sha512-7maUZy1N7uo6+WVEX6psASxtNlKaNVMlGQKkG/63nEDdLOWNbiUMoLK7X4uYoLhQstau72mLgfEWcXcwsaHbYQ==",
      "dependencies": {
        "ip": "^2.0.0",
        "smart-buffer": "^4.2.0"
      },
      "engines": {
        "node": ">= 10.13.0",
        "npm": ">= 3.0.0"
      }
    },
    "node_modules/sparse-bitfield": {
      "version": "3.0.3",
      "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz",
      "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==",
      "optional": true,
      "dependencies": {
        "memory-pager": "^1.0.2"
      }
    },
    "node_modules/tr46": {
      "version": "3.0.0",
      "resolved": "https://registry.npmjs.org/tr46/-/tr46-3.0.0.tgz",
      "integrity": "sha512-l7FvfAHlcmulp8kr+flpQZmVwtu7nfRV7NZujtN0OqES8EL4O4e0qqzL0DC5gAvx/ZC/9lk6rhcUwYvkBnBnYA==",
      "dependencies": {
        "punycode": "^2.1.1"
      },
      "engines": {
        "node": ">=12"
      }
    },
    "node_modules/undici-types": {
      "version": "5.25.3",
      "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.25.3.tgz",
      "integrity": "sha512-Ga1jfYwRn7+cP9v8auvEXN1rX3sWqlayd4HP7OKk4mZWylEmu3KzXDUGrQUN6Ol7qo1gPvB2e5gX6udnyEPgdA=="
    },
    "node_modules/webidl-conversions": {
      "version": "7.0.0",
      "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz",
      "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==",
      "engines": {
        "node": ">=12"
      }
    },
    "node_modules/whatwg-url": {
      "version": "11.0.0",
      "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-11.0.0.tgz",
      "integrity": "sha512-RKT8HExMpoYx4igMiVMY83lN6UeITKJlBQ+vR/8ZJ8OCdSiN3RwCq+9gH0+Xzj0+5IrM6i4j/6LuvzbZIQgEcQ==",
      "dependencies": {
        "tr46": "^3.0.0",
        "webidl-conversions": "^7.0.0"
      },
      "engines": {
        "node": ">=12"
      }
    }
  }
}

My app.js

const { MongoClient } = require("mongodb");

// Replace the uri string with your connection string.
const uri = "mongodb://localhost:27017";

const client = new MongoClient(uri);

async function run() {
  try {
    const database = client.db('fruitsDb');
    const fruits = database.collection('fruits');

    // Query for a Banana
    const query = { name: 'Banana' };
    const banana = await fruits.findOne(query);

    console.log(banana);
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);

I have tried delete node_modules and package_lock.json and restart. Have tried to downgrad the mongodb version to ^5.2.0. I don’t know what else to do :frowning: please SOS

The syntax error seems to come from the file add_user.js.

Could you please share this file.

Its a file from mongo_db!. The path is “/home/monicaveloso/about/FruitsProject/node_modules/mongodb/lib/operations/add_user.js:16”

The file is

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddUserOperation = void 0;
const crypto = require("crypto");
const error_1 = require("../error");
const utils_1 = require("../utils");
const command_1 = require("./command");
const operation_1 = require("./operation");
/** @internal */
class AddUserOperation extends command_1.CommandCallbackOperation {
    constructor(db, username, password, options) {
        super(db, options);
        this.db = db;
        this.username = username;
        this.password = password;
        this.options = options ?? {};
    }
    executeCallback(server, session, callback) {
        const db = this.db;
        const username = this.username;
        const password = this.password;
        const options = this.options;
        // Error out if digestPassword set
        // v5 removed the digestPassword option from AddUserOptions but we still want to throw
        // an error when digestPassword is provided.
        if ('digestPassword' in options && options.digestPassword != null) {
            return callback(new error_1.MongoInvalidArgumentError('Option "digestPassword" not supported via addUser, use db.command(...) instead'));
        }
        let roles;
        if (!options.roles || (Array.isArray(options.roles) && options.roles.length === 0)) {
            (0, utils_1.emitWarningOnce)('Creating a user without roles is deprecated. Defaults to "root" if db is "admin" or "dbOwner" otherwise');
            if (db.databaseName.toLowerCase() === 'admin') {
                roles = ['root'];
            }
            else {
                roles = ['dbOwner'];
            }
        }
        else {
            roles = Array.isArray(options.roles) ? options.roles : [options.roles];
        }
        let topology;
        try {
            topology = (0, utils_1.getTopology)(db);
        }
        catch (error) {
            return callback(error);
        }
        const digestPassword = topology.lastHello().maxWireVersion >= 7;
        let userPassword = password;
        if (!digestPassword) {
            // Use node md5 generator
            const md5 = crypto.createHash('md5');
            // Generate keys used for authentication
            md5.update(`${username}:mongo:${password}`);
            userPassword = md5.digest('hex');
        }
        // Build the command to execute
        const command = {
            createUser: username,
            customData: options.customData || {},
            roles: roles,
            digestPassword
        };
        // No password
        if (typeof password === 'string') {
            command.pwd = userPassword;
        }
        super.executeCommandCallback(server, session, command, callback);
    }
}
exports.AddUserOperation = AddUserOperation;
(0, operation_1.defineAspects)(AddUserOperation, [operation_1.Aspect.WRITE_OPERATION]);
//# sourceMappingURL=add_user.js.map

This is valid JS according to MDN. I am puzzled. I have try it in a normal (that is not a constructor) and it worked fine.

I have tagged your post with node-js in hope of somebody with more JS experience see it.

So, still trying I discovery part of the problem.

As shown in my first message output:

monicaveloso@SJC-UBU-001459:~/about/FruitsProject$ node app.js
(node:1436578) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
null
monicaveloso@SJC-UBU-001459:~/about/FruitsProject$ nodejs app.js
/home/monicaveloso/about/FruitsProject/node_modules/mongodb/lib/operations/add_user.js:16
        this.options = options ?? {};
                                ^

SyntaxError: Unexpected token ?

The node app.js works just fine, but mongodb is giving me just null instead of working or thrown an error, and it makes me think that node wasnt working. Then I tried to run with nodejs, and nodejs isnt in the same version than node. Giving me the sintax error.

So its just a problem of discovering why mongodb is giving me the null output. (my app.js is described in my first comment).

If I understand the following correctly

you want to know why the const banana is evaluated to null in the following code:

I think you simply forgot to open the connection.

I am sorry if I focused on the syntax error rather than on the null banana issue but

  1. your first post was in a thread with the title Getting Error This.options = options ? {};,
  2. your first line of text mentioned I’m having this error too
  3. and your first screenshot shown a stack trace with the error this.options = options ?? {};

So I really really thought that the issue was about the syntax error with ??. You should have created a new thread with a title like My query returns null. Why?

To terminate, if the issue is not the missing open(), the null banana could be that

  1. you are connecting to the wrong server
  2. you are using the wrong database
  3. you are querying the wrong collection
  4. there is no document in given server/database/collection with a field named name with the value Banana.

My initial error was the one I initially reported. However, I persisted to resolve the issue, and I eventually identified the problem. The first line of my previous message was:

“So, still trying I discovery part of the problem.”

I discovered that when I ran “nodejs app.js,” it was giving me a problem related to ‘this.options = options ?? {};’ because the version of Node.js I had installed differed from ‘node.’

Running “node app.js” yielded a null result, which led me to use the ‘nodejs’ command instead, mistakenly thinking that “node app.js” wasn’t working because of null result.

I have no intention of shifting away from the original problem, so I will open another forum thread if the need arises.

Thank you for your attention!