简体   繁体   English

使用节点和猫鼬导出时出错

[英]Error while exporting using node and mongoose

So, I have create some schema like following and exporting the model,所以,我创建了一些模式,比如关注和导出模型,

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**/

This works fine.这工作正常。

However later i thought that I would like to export the schema also from this js file, so i changed the last line as follows:但是后来我想我也想从这个 js 文件中导出模式,所以我将最后一行更改如下:

module.exports = {Doctor,doctorSchema}

My code started failing, then i realised that if I write我的代码开始失败,然后我意识到如果我写

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

my code fails again.我的代码再次失败。

This is how we export in node ?这就是我们在节点中导出的方式? right?对? but this is failing my code.但这使我的代码失败。

How are you importing the schema?你是如何导入架构的? You should extract the schema name using the dot notation since you are exporting an object.由于您要导出对象,因此您应该使用点表示法提取模式名称。

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

You can export model and schema as follows:您可以按如下方式导出模型和架构:

First Option:第一个选项:

module.exports = Doctor

Import as导入为

const Doctor = require('exportedSchemaFilePath')

Second Option:第二种选择:

module.exports = {Doctor,doctorSchema}

Import as导入为

const {Doctor, DoctorSchema} = require('exportedSchemaFilePath')

As you are exporting as JSON object当您导出为 JSON 对象时

Third option:第三个选项:

module.exports = {Doctor} 

import as导入为

const {Doctor} = require('exportedSchemaFilePath')

You just need to change require option as you change export methods您只需要在更改导出方法时更改 require 选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM