首页 > 解决方案 > 使用节点和猫鼬导出时出错

问题描述

所以,我创建了一些模式,比如跟踪和导出模型,

var mongoose = require('mongoose');


var specSchema = new mongoose.Schema({
    name: String,
    description:String
});

var qualSchema = new mongoose.Schema({
    name: String,
    description:String
});


var doctorSchema = new mongoose.Schema({
    name: String,

    // qualifications:[qualSchema],
    // specializations:[specSchema]
});

var Doctor = mongoose.model('Doctor',doctorSchema);
module.exports = Doctor/**please see here**/

这工作正常。

但是后来我想我也想从这个 js 文件中导出模式,所以我将最后一行更改如下:

module.exports = {Doctor,doctorSchema}

我的代码开始失败,然后我意识到如果我写

module.exports = {Doctor} /**i.e add curly braces to it**/

我的代码再次失败。

这就是我们在节点中导出的方式?正确的?但这使我的代码失败。

标签: javascriptnode.jsmongodbexpressmongoose

解决方案


你是如何导入架构的?由于要导出对象,因此应使用点表示法提取模式名称。

const Doctor = require('exportedSchemaPath').Doctor;

推荐阅读