这是我的文件夹 struct :

+-- express_example
|---- app.js
|---- models
|-------- songs.js
|-------- albums.js
|---- and another files of expressjs

我在文件歌曲中的代码.js

var mongoose = require('mongoose')
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;

var SongSchema = new Schema({
name: {type: String, default: 'songname'}
, link: {type: String, default: './data/train.mp3'}
, date: {type: Date, default: Date.now()}
, position: {type: Number, default: 0}
, weekOnChart: {type: Number, default: 0}
, listend: {type: Number, default: 0}
});

module.exports = mongoose.model('Song', SongSchema);

这是我在文件相册中的代码.js

var mongoose = require('mongoose')
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;

var AlbumSchema = new Schema({
name: {type: String, default: 'songname'}
, thumbnail: {type:String, default: './images/U1.jpg'}
, date: {type: Date, default: Date.now()}
, songs: [SongSchema]
});

module.exports = mongoose.model('Album', AlbumSchema);


How can I make albums.js know SongSchema to be defined AlbumSchema

推荐答案

您可以直接使用Mongoose在其他地方定义模型:

require('mongoose').model(name_of_model)

在相册中获取示例中的模式.你可以这样做:

var SongSchema = require('mongoose').model('Song').schema

Node.js相关问答推荐

正在try 使用Azure Function App上载文件时未上载文件(&Q;)

MongoDB如果文档S的字段小于另一个字段,则将该字段加一

try 使用Express和连接池将数据插入MySQL数据库时出现拒绝访问错误

Node js 处理回调和 Promise

如何在MongoDB中删除嵌套对象数组中的属性?

如何获取需要加载cheerio的网站部分数据?

Mocha调用所有it回调模拟(测试中间件)

更新文档数组中的文档 Mongoose

如何在不使用位置运算符 $ 的情况下更新 mongodb 文档中数组中的嵌套文档?

Pug - 包括带有include关键字的c代码

AWS EC2 npm install 突然很慢

配额超出了每分钟的 Sheets API 写入请求数. node .js

如何在 cypress 测试中进行计算

在 ExpressJS 中将变量传递给 JavaScript

来自 Node-aws 的 Dynamo Local:所有操作都失败无法对不存在的表执行操作

在多个 .env 文件之间切换,例如 .env.development 和 node.js

使用 Node.js 和 Express 进行简单的 API 调用

npm install - javascript堆内存不足

yarn 和 npm 的主要区别是什么?

桌面应用程序仅支持 oauth_callback 值 'oob'/oauth/request_token