Node.js 导入网页包导出mongoose架构
我在两个包之间工作。在第一个示例中,我定义了mongoose(v5.4.19)模式:Node.js 导入网页包导出mongoose架构,node.js,webpack,mongoose,babeljs,babel-loader,Node.js,Webpack,Mongoose,Babeljs,Babel Loader,我在两个包之间工作。在第一个示例中,我定义了mongoose(v5.4.19)模式: //src/models/index.js 从“mongoose/浏览器”导入mongoose const mySchema=newmongoose.Schema({ 名称:String, 物品清单:[] }) 导出默认mySchema 我使用标准的网页配置将其构建到dist文件中: //webpack.config.js const path=require('路径') module.exports={ 条
//src/models/index.js
从“mongoose/浏览器”导入mongoose
const mySchema=newmongoose.Schema({
名称:String,
物品清单:[]
})
导出默认mySchema
我使用标准的网页配置将其构建到dist文件中:
//webpack.config.js
const path=require('路径')
module.exports={
条目:{
组件:'./src/components/index.js',
模型:'./src/models/index.js',
},
输出:{
path:path.resolve(uu dirname,'dist'),
文件名:'[name].js',
libraryTarget:“commonjs2”
},
模式:“生产”,
模块:{
规则:[
{
测试:/\.js$/,,
排除:/node_模块/,
使用:{
加载器:“巴别塔加载器”,
}
}
],
},
};
而.babelrc
只加载env预置和两个插件(objectrest-spread、react-jsx)
在第二个包(Next.js应用程序)中,我导入了该模式:
//pages/index.js
从“React”导入React
从“mongoose/浏览器”导入mongoose
从“myfirstlib/dist/models”导入mySchema
/*
const myDuplicateSchema=新的mongoose.Schema({
名称:String,
物品清单:[]
})
*/
const myDoc=新猫鼬文件({
姓名:'姓名',
物品清单:[]
},mySchema)
导出默认值()=>
当我尝试填充listOfThings
prop时,我收到以下错误:
TypeError:无法读取未定义的属性“$\”
它出现在我的网页包模型中的某个地方(dist/models.js
):
- 如果我不尝试为我的
物品列表指定一个值,它就可以正常工作
- 如果我使用
而不是myDuplicateSchema
初始化mySchema
(即使myDoc
是非空数组),它也可以正常工作listOfThings
- 我还可以使用
newmongoose.Schema(courseSchema.obj)
console.log(mySchema,myDuplicateSchema)
时,它们显示为具有相同内容但具有不同“类型”的JS对象(我不确定我在这里使用的词是否正确):第一个作为$
打印,而第二个作为模式打印
因此,如果我理解正确,当模式在我的第一个包中进行网页打包时,它会丢失一些东西,使它无法在我的第二个包中使用。我尝试添加列出的webpack配置选项,但结果相同
我能做些什么使我的模式在我的第二个包中可用吗
691 | /*!
692 | * ignore
> 693 | */t.exports=function(t,e,n){const r=(n=n||{}).skipDocArrays;let i=0;for(const n of Object.keys(t.$__.activePaths.states.modify)){if(r){const e=t.schema.path(n);if(e&&e.$isMongooseDocumentArray)continue}0===n.indexOf(e+".")&&(delete t.$__.activePaths.states.modify[n],++i)}return i}},function(t,e,n){"use strict";
694 | /*!
695 | * Module dependencies.
696 | */const r=n(21).get().Binary,i=n(1),o=n(48).Buffer,s=o.from("").constructor.prototype;function a(t,e,n){let r,s,c,u,l;return r=0===arguments.length||null===arguments[0]||void 0===arguments[0]?0:t,Array.isArray(e)?(c=e[0],u=e[1]):s=e,l="number"==typeof r||r instanceof Number?o.alloc(r):o.from(r,s,n),i.decorate(l,a.mixin),l.isMongooseBuffer=!0,Object.defineProperties(l,{validators:{value:[],enumerable:!1},_path:{value:c,enumerable:!1},_parent:{value:u,enumerable:!1}}),u&&"string"==typeof c&&Object.defineProperty(l,"_schema",{value:u.schema.path(c)}),l._subtype=0,l}