Unable to initialize MongoClient

Attempting to bring the MongoClient into my Vue project without triggering an error.

const MongoClient = require("mongodb").MongoClient;

Error message in the browser:

> utils.js?071e:543 Uncaught TypeError: Cannot read property 'split' of undefined
at Object.eval (utils.js?071e:543)
at eval (utils.js:913)
at Object../node_modules/mongodb/lib/utils.js (chunk-vendors.js:5077)
at __webpack_require__ (app.js:854)
at fn (app.js:151)
at eval (read_preference.js?4638:2)
at Object../node_modules/mongodb/lib/core/topologies/read_preference.js (chunk-vendors.js:3997)
at __webpack_require__ (app.js:854)
at fn (app.js:151)
at eval (shared.js?2650:3)

Error in the browser console

Upon inspecting the code at /node_modules/mongodb/lib/utils.js, a single split() operation was found:

...const nodejsMajorVersion = +process.version.split('.')[0].substring(1);

Realized that process.version (and even process) are undefined. So, I set:

const nodejsMajorVersion = 14;

This resolved the issue, but led to another one:

> defaultable.js?6987:55 Uncaught Error: Cannot find module 'net'
at webpackEmptyContext (eval at ./node_modules/defaultable sync recursive (app.js:1088), <anonymous>:2:10)
at Object.workaround_require (defaultable.js?6987:49)
at require (defaultable.js?6987:77)
at eval (server.js?663d:9)
at defaulter (defaultable.js?6987:83)
at defaultable (defaultable.js?6987:63)
at good (defaultable.js?6987:174)
at Object.eval (server.js?663d:5)
at eval (server.js:242)
at Object../node_modules/hbo-dnsd/server.js (chunk-vendors.js:3106)

Error in the browser console

node --version          --> v14.17.1
npm mongodb --version   --> 6.14.13

Tried reinstalling both node and mongodb already. Yet, the issues persist.

Package.json:

{
  "name": "snlh_no_ts",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "test:unit": "vue-cli-service test:unit",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "core-js": "^3.6.5",
    "dns": "^0.2.2",
    "mongodb": "^3.6.9",
    "mongodb-client-encryption": "^1.2.5",
    "vue": "^2.6.11",
    "vue-router": "^3.2.0",
    "vuex": "^3.4.0"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-plugin-router": "~4.5.0",
    "@vue/cli-plugin-unit-jest": "~4.5.0",
    "@vue/cli-plugin-vuex": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "@vue/eslint-config-prettier": "^6.0.0",
    "@vue/test-utils": "^1.0.3",
    "babel-eslint": "^10.1.0",
    "eslint": "^6.7.2",
    "eslint-plugin-prettier": "^3.3.1",
    "eslint-plugin-vue": "^6.2.2",
    "prettier": "^2.2.1",
    "sass": "^1.26.5",
    "sass-loader": "^8.0.2",
    "vue-template-compiler": "^2.6.11"
  }
}

Any suggestions on how to resolve these issues?

Answer №1

It appears that you are attempting to import mongodb on the client-side (front-end), which operates in a web browser rather than in the node.js environment required for mongodb functionality.

To resolve this issue, you will need to set up a node.js server to establish a connection with your database and facilitate communication via an API with your front-end interface.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The issue of Bootstrap modal not dynamically updating with Vue.js v2 two-way binding

I'm a beginner in working with Vue.js and attempting to integrate it with a Bootstrap modal for displaying more detailed data. My situation involves a table with multiple records and a button to view extensive information for the selected record withi ...

Deactivate every textarea within a loop when adding a comment to a post using Vue.js

How can I modify my code so that only one text area opens at a time when I click the comment button instead of all text areas opening simultaneously? // Sample View <div id="app" v-cloak> <div v-for="post ,key in posts" > @{{post.content ...

Guide to transferring a calculated outcome to a parameter in vue2.js

Is it possible to pass a computed result as a parameter in Vue.js? <el-input v-model="ruleForm.volexp"/>{{ calVol }} = {{ ruleForm.vol }} data() { return { ruleForm: { volexp: '', vol: 0 } } }, computed: { calVol: function() { ...

Understanding the fundamentals of Handlebars IF statements

I am trying to accomplish a simple task in HBS, but I have hit a roadblock. How can I write a conditional statement for when the value is greater than 0? {{#if value > 0}} {{/if}} Additionally, does anyone have recommendations for a good HBS tutorial ...

How can we implement the MUI snackbar to only show when a successful login occurs in ReactJS?

How can I display the material-ui snackbar in ReactJS only upon successful login? What approaches can be used to achieve this in ReactJS? ...

Unable to adjust the width of the react-select component

I've been struggling to adjust the width of the select element but no matter what I try, it remains at a default width of about 75px. const CustomContainer = styled('div')` width: 100%; height: 100%; display: flex; flex-flow: row wr ...

error message: "The CSRF token is either missing or incorrect, please try again."

I implemented window.CSRF_TOKEN = "{{ csrf_token }}" within a script tag in restaurant_detail.html where my react page for posting reviews is rendered. However, I encountered an error. Upon checking in my onSubmit function, I confirmed that the csrf token ...

Facing an issue with postcss and autoprefixer where the error message "module not found" is displayed

I'm attempting to utilize npm scripts for adding prefixes. Below is the npm script I'm currently using: postcss --use autoprefixer prodapp/css/sass.css -d prodapp/css/ They have also provided the same command on their GitHub page. https://gith ...

Is there a way to modify the image of a single face in a Cubemap using THREE.js?

In my project, I am attempting to showcase a panorama using cubemaps by initially loading and displaying 6 low-quality images on the cubemap. Achieving this can be easily done through the following code: var urls = [ 'path/to/low-q-pos-x.png', & ...

Error: The Jquery plugin Object #<HTMLDocument> does not have a function called 'observe'

Hello, I recently tried to install the Jquery plugin known as chosen that allows customization of my <select> tag across different browsers. If you're interested, you can find more information about it here. However, after integrating this plugi ...

Stargazing as I arrange my collection based on a deeply nested element

Within my application, there is a list of tiles representing each project in a portfolio. This serves as the main list view for the app where all projects are fetched from the collection without any specific sorting or ordering. If an optional slug parame ...

Adding a unique key to every element within a JavaScript array

I am working with the array provided below which contains simple values. My goal is to add a key id before each value in the array, resulting in something like this: ["id:a", "id:b","id:c","id:d"]. Is there an easy way to achieve this? Any assistance would ...

Refining data within table rows based on selected options

Currently, I am facing an issue with filtering a table using a select option. After the initial filter, I am unable to show all the data again. Additionally, I am seeking assistance in making my code more concise. // STRUGGLING TO SHOW ALL DATA AFTER INIT ...

What is the best way to combine two JavaScript objects?

One object I am currently working with resembles the following structure let ob1 = { 'item_data':{ 'stack':{ 'purchase':'12345', 'order':'22222' } } } Additionally, I have anothe ...

To access a different page, simply click the link and update the class on the new page

I am facing an issue with the tabs on my homepage. Currently, only one tab with the "current" class is visible while the rest are hidden. Is there a way to link from pageX to my homepage and specify which tab should be open? For instance, when clicking o ...

Change the controller's classes and update the view in Angular

In my angular application, I have a popup window containing several tabs. These tabs are styled like Bootstrap and need to be switched using Angular. The code for the tabs is as follows (simplified): <div class="settingsPopupWindow"> <div> ...

Transform JSON data into an HTML layout

I'm looking to design a structure that showcases JSON information using HTML div elements. For example, utilizing the h4 tag for headers, p tag for text descriptions, and img tag for images. Can anyone provide guidance on the most efficient approach ...

Issue with primeng dropdown not displaying the selected label

When using the editable dropdown with filter feature from PrimeFaces, I've noticed that selecting an option displays the value instead of the label. https://i.sstatic.net/8YFRa.png Here is the code snippet: <div class="col-md-5 col-xs-1 ...

Why does my ball defy the laws of geometry and refuse to be perfectly round with

I'm attempting to create a simple game in javascript, but I've run into an issue where my circle isn't perfectly round. Despite searching for solutions online for 20 minutes, I haven't been able to resolve the problem. I'm hoping s ...

"Exploring the functionality of HTML buttons on iOS Safari with Angular click

Currently, I am developing a web app that includes a feature where users can hold down a button to adjust a value. The backend of the app is supported by Meteor.js with Angular serving as the front end. The functionality works perfectly, except for Mobile ...