使用Webpack与Mongoose - mongoose.model不是一个功能 [英] Using Webpack with Mongoose - mongoose.model is not a function
问题描述
未捕获TypeError:mongoose.model不是一个函数
在对象<匿名> (bundle.js:44338)
在__webpack_require__(bundle.js:20)
在Object。< anonymous> (bundle.js:48)
在Object。< anonymous> (bundle.js:68)
在__webpack_require__(bundle.js:20)
在bundle.js:40
在bundle.js:43
我知道mongoose根据 http://mongoosejs.com/docs/browser.html 和我甚至包括建议的脚本标记,仍然收到错误。
我还使用mongoose正确导出了我的模块:
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 = {
条目:./main.js,
输出:{
路径:__dirname,
filename:bundle .js
},
模块:{
装载机:[
{test:/\ json$/,loader:'json-loader'}
]
},
resolve:{
extensions:['','.webpack.js','.web.js','.js','.json']
$,
node:{
控制台:'空',
fs:'空',
net:'空'
}
}
为什么会发生这种情况?如何解决?
mongoose.model只是服务器端。在客户端/浏览器上没有模型,因为客户端/浏览器没有数据库连接。您可以使用mongoose.Document,为了保持模式DRY / isomorph。
http://mongoosejs.com/docs/unstable/docs/browser.html
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?
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
这篇关于使用Webpack与Mongoose - mongoose.model不是一个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!