Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

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

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...