簡體   English   中英

將Webpack與Mongoose結合使用-mongoose.model不是函數

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

我正在使用Webpack在瀏覽器中使用require模塊; 但是,出現以下錯誤:

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

根據http://mongoosejs.com/docs/browser.html ,我知道貓鼬在瀏覽器中有一些限制, 我什至包括了建議的腳本標記,仍然收到錯誤

我還使用貓鼬正確導出了我的模塊:

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);

我的webpack.config.js也已正確配置

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'
    }
}

為什么會這樣呢? 以及如何解決?

mongoose.model僅在服務器端。 在客戶端/瀏覽器上沒有模型,因為客戶端/瀏覽器沒有數據庫連接。 但是,您可以使用mongoose.Document,以保持架構DRY / isomorph。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM