Using Webpack with Mongoose - mongoose.model is not a function

I use Webpack to use the required modules in the browser; however, I get the following error:

Uncaught TypeError: mongoose.model is not a function
    at Object.<anonymous> (bundle.js:44338)
    at __webpack_require__ (bundle.js:20)
    at Object.<anonymous> (bundle.js:48)
    at Object.<anonymous> (bundle.js:68)
    at __webpack_require__ (bundle.js:20)
    at bundle.js:40
    at bundle.js:43

I know that mongoose has some limitations in the browser according to http://mongoosejs.com/docs/browser.html and I even turned on the script tag and still get the error .

I also exported my module using mongoose correctly:

var mongoose  = require('mongoose');
var Schema  = mongoose.Schema;

var ProductSchema = new Schema({
    imageURL: String,
    productName: String,
    productType: String,
    price: String
}, {collection: 'products'});

module.exports = mongoose.model('products', ProductSchema);

My webpack.config.js is also configured correctly.

var webpack = require('webpack');
var path = require('path');
var fs = require('fs');
var request = require('request');
var CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');

module.exports = {
    entry: "./main.js",
    output: {
        path: __dirname,
        filename: "bundle.js"
    },
    module: {
    loaders: [
      { test: /\.json$/, loader: 'json-loader' }
        ]
    },
    resolve: {
        extensions: ['', '.webpack.js', '.web.js', '.js', '.json']
    },
    node: {
        console: 'empty',
        fs: 'empty',
        net: 'empty',
        tls: 'empty'
    }
}

Why is this happening? And how can this be solved?

0
source share
1 answer

mongoose.model - . / , /browswer . mongoose.Document, , DRY/isomorph.

http://mongoosejs.com/docs/unstable/docs/browser.html

0

Source: https://habr.com/ru/post/1655225/


All Articles