我正在尝试使用browserify将我的mongoose模式导入到一个js文件中,我计划在客户端使用它。这里有两个模式:answer.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const answerSchema = new Schema({
body : String,
likes : Number,
author : {
type : Schema.Types.ObjectId,
ref : 'User'
},
likedBy : [
{
type : Schema.Types.ObjectId,
ref : 'User'
}
]
});
module.exports = mongoose.model("Answer",answerSchema);
问题.js
const mongoose = require('mongoose');
const Answer = require('./answer');
const Schema = mongoose.Schema;
const QuestionSchema = new Schema({
title : String,
description : String,
likes : Number,
author : {
type : Schema.Types.ObjectId,
ref : 'User'
},
answers : [
{
type : Schema.Types.ObjectId,
ref : 'Answer'
}
],
likedBy : [
{
type : Schema.Types.ObjectId,
ref : 'User'
}
]
});
QuestionSchema.post('findOneAndDelete', async function(doc) {
if(doc) {
await Answer.deleteMany({
_id : {
$in : doc.answers
}
});
}
});
module.exports = mongoose.model('Question',QuestionSchema);
我使用browserify在一个名为islike.js的文件中使用它们。我在linux终端上写了以下内容
browserify -r ./question.js:questionModel -r ./answer.js:answerModel > ../public/js/isLikedBundle.js
在show.ejs文件中,我执行了以下操作:
<script src="/js/isLikedBundle.js"></script>
<script src="/js/isLiked.js"></script>
为了测试是否一切正常,我在isliked.js中执行了以下操作:
const Question = require('questionModel');
const Answer = require('answerModel');
console.log(Question);
console.log(Answer);
我在控制台上得到了以下输出:
ƒ n(t,o){return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,n),e.call(this,t,r,o)}
ƒ n(t,o){return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,n),e.call(this,t,r,o)}
我不能使用任何 Mongoose 函数。请帮忙!!!
暂无答案!
目前还没有任何答案,快来回答吧!