Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm using Webpack to use require modules in the browser; however, I'm getting 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 mongoose has some limitations in the browser according to http://mongoosejs.com/docs/browser.html and I even included the script tag suggested and still receiving the error.

I have 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 configured correctly as well

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 occurring? And how can this be resolved?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
121 views
Welcome To Ask or Share your Answers For Others

1 Answer

mongoose.model is server side only. On the client/browser there is no model, because the client/browswer has no database connection. You can use mongoose.Document though, in order to keep schemas DRY/isomorph.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...