phpreactjswordpressmongodbphp-mongodb

Uncaught Error: Class "MongoDB\Client" not found in


I am creating custom divi module and want to fetch data from mongodb database and display it accordingly.

Here is full log of the error:

Fatal error: Uncaught Error: Class "MongoDB\Client" not found in C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\plugins\test\includes\modules\Exams\Exams.php:53 Stack trace: #0 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\plugins\test\includes\modules\Exams\Exams.php(37): Exams->get_mongodb_data() #1 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\class-et-builder-element.php(3433): Exams->render(Array, '', 'test_exams', '', '', '', '', '') #2 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(433): ET_Builder_Element->render(Array, '', 'test_exams') #3 [internal function]: do_shortcode_tag(Array) #4 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(273): preg_replace_callback('/\[(\[?)(et_pb...', 'do_shortcode_ta...', '[et_pb_text _bu...') #5 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\main-structure-elements.php(3784): do_shortcode('[et_pb_text _bu...') #6 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\class-et-builder-element.php(3433): ET_Builder_Column->render(Array, '[et_pb_text _bu...', 'et_pb_column', '', '', '', '', '') #7 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(433): ET_Builder_Element->_render(Array, '[et_pb_text bu...', 'et_pb_column') #8 [internal function]: do_shortcode_tag(Array) #9 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(273): preg_replace_callback('/\[(\[?)(et_pb...', 'do_shortcode_ta...', '[et_pb_column t...') #10 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\main-structure-elements.php(2274): do_shortcode('[et_pb_column t...') #11 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\class-et-builder-element.php(3433): ET_Builder_Row->render(Array, '[et_pb_column t...', 'et_pb_row', '', '', '', '', '') #12 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(433): ET_Builder_Element->render(Array, '[et_pb_column t...', 'et_pb_row') #13 [internal function]: do_shortcode_tag(Array) #14 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(273): preg_replace_callback('/\[(\[?)(et_pb...', 'do_shortcode_ta...', '[et_pb_row colu...') #15 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\main-structure-elements.php(1606): do_shortcode('[et_pb_row colu...') #16 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\class-et-builder-element.php(3433): ET_Builder_Section->render(Array, '[et_pb_row colu...', 'et_pb_section', '', '', '', '', '') #17 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(433): ET_Builder_Element->render(Array, '[et_pb_row colu...', 'et_pb_section') #18 [internal function]: do_shortcode_tag(Array) #19 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\shortcodes.php(273): preg_replace_callback('/\[(\[?)(et_pb...', 'do_shortcode_ta...', '<div class="et-...') #20 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\class-wp-hook.php(324): do_shortcode('<div class="et-...') #21 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\plugin.php(205): WP_Hook->apply_filters('<div class="et-...', Array) #22 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\core.php(26): apply_filters('et_builder_rend...', '[et_pb_section ...') #23 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\frontend-builder\theme-builder\frontend.php(347): et_builder_render_layout('[et_pb_section ...') #24 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\frontend-builder\theme-builder\frontend.php(506): et_theme_builder_frontend_render_layout('et_body_layout', 8) #25 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\themes\Divi\includes\builder\frontend-builder\theme-builder\frontend-body-template.php(10): et_theme_builder_frontend_render_body(8, true, false) #26 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-includes\template-loader.php(106): include('C:\Users\qhuser...') #27 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-blog-header.php(19): require_once('C:\Users\qhuser...') #28 C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\index.php(17): require('C:\Users\qhuser...') #29 {main} thrown in C:\Users\qhuser\Local Sites\wordpress-with-react\app\public\wp-content\plugins\test\includes\modules\Exams\Exams.php on line 53

My .php file looks like this:

<?php

use MongoDB\Client;

class Exams extends ET_Builder_Module
{

    public $slug       = 'test_exams';
    public $vb_support = 'on';

    protected $module_credits = array(
        'module_uri' => '',
        'author'     => 'Ilia',
        'author_uri' => '',
    );

    public function init()
    {
        $this->name = esc_html__('Exams', 'test-test');
    }

    public function get_fields()
    {
        return array(
            'content' => array(
                'label'           => esc_html__('Content', 'test-test'),
                'type'            => 'tiny_mce',
                'option_category' => 'basic_option',
                'description'     => esc_html__('Content entered here will appear inside the module.', 'test-test'),
                'toggle_slug'     => 'main_content',
            ),
        );
    }

    public function render($attrs, $content = null, $render_slug)
    {
        $mongodb_data = $this->get_mongodb_data();

        // Pass MongoDB data to React component
        $props = array(
            'content' => $content,
            'mongodbData' => $mongodb_data,
        );

        return $this->shortcode_content($props);
        // return sprintf( '<h1>%1$s</h1>', $this->props['content'] );
    }

    private function get_mongodb_data()
    {
        $uri = "mongodb://production:23yPum0%7B4O4y%3Eg%27%26b%3Fz%21@37.60.245.139:27017/?authSource=admin";

        $client = new Client($uri);

        $db = $client->selectDatabase("testyourself");
        $collection = $db->selectCollection("examtypes");

        $result = $collection->find();

        $data = [];
        foreach ($result as $document) {
            $data[] = $document;
        }

        return $data;
    }
}

new Exams ?>

<? phpinfo() ?>

My .jsx file looks like this:

// External Dependencies
import React, { Component } from "react";

// Internal Dependencies
import "./style.css";
class Exams extends Component {
  static slug = "test_exams";

  render() {
    const Content = this.props.content();
    const MongoDBData = this.props.mongodbData;

    return (
      <div>
        <h1>
          <Content />
        </h1>
        <div className="mongodb-data">
          {/* {MongoDBData.map((document, index) => console.log(document))} */}
          Hello
        </div>
      </div>
    );
  }
}

export default Exams;

I started by including mongodb in php.ini files, installation of composer + php plugin for mongo which I included into the xampp/php/ext folder. Also I ran the composer require mongodb/mongodb command in order to setup mongodb into my custom module. But now when I try to use the Exams module (which is present in the divi builder) I ran into the Uncaught Error: Class "MongoDB\Client" not found in... fatal error.

When running the php.info() function it doesn't display any support for MongoDB and says that the Loaded Configuration File is located inside C:\Users\qhuser\AppData\Roaming\Local\run\5khIdGuD6\conf\php\php.ini. However, when I run the php -m command inside powershell, I can see that mongodb is there. When executing the php --ini command, the Loaded Configuration File is located inside C:\xampp\php\php.ini. It may be a problem with configuration or something?

Also I am in development right now, but want to use that module in other websites, so how can I setup everything to work properly in other website too, without additional (or with minimal) changes and without doing setup for several php.ini files?

Here are all my folders and files:

enter image description here


Solution

  • You need to include or require the class file. If you installed the library via composer, require the autoloader:

    <?php
    // This path should point to Composer's autoloader
    require 'vendor/autoload.php';
    

    doc