我有一个架构设计,用户可以在其中提交语言翻译请求。翻译请求文档引用了用户文档,属性被称为" _requester"和" _翻译"。我希望能够在对翻译请求集合执行某些查询时填充这些内容。当我执行某个查询时,会出现问题,填充_translator并且结果看起来很完美。然后,如果我执行完全相同的查询,则第二次失败,并显示错误消息架构尚未注册模型"用户"。
以下是我的代码编码方式。有两个文件。 mongoose所在的一个js文件和另一个通过模型使用它的js文件,就像依赖注入一样。
(适用猫鼬-util.js中)
function MongooseUtil(){
var mongoose = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');
var Schema = {
User: {
userId: String,
password: String,
nickName: String,
state: String,
emailVerificationCode: String,
avatarUrl: String,
points: Number
},
TranslationRequest: {
_requester: { type: mongoose.Schema.ObjectId, ref: "User" },
_translator: { type: mongoose.Schema.ObjectId, ref: "User" },
to: String,
from: String,
textToTranslate: String,
translatedText: String,
state: String,
requestedTime: Number,
translatedTime: Number,
gradedTime: Number,
grade: Number,
requesterUserId: String,
translatorUserId: String,
graderUserId: String
}
};
// user
var userSchema = mongoose.Schema(Schema.User);
var userModel = mongoose.model('User', userSchema);
// translation request
var translationRequestSchema = mongoose.Schema(Schema.TranslationRequest);
translationRequestSchema.plugin(mongoosePaginate);
var translationRequestModel = mongoose.model('TranslationRequest', translationRequestSchema);
this.getUserModel = function(){
return userModel;
}
this.getTranslationRequestModel = function(){
return translationRequestModel;
}
}
module.exports = new MongooseUtil();
(适用翻译请求-service.js)
function TranslationRequestService(){
var mongooseUtil = require('../../../utils/mongoose-util/mongoose-util');
var TranslationRequest = mongooseUtil.getTranslationRequestModel();
var _this = this;
this.getTranslationResultById = function(id){
return TranslationRequest.findOne({ _id: id })
.populate('_translator', 'nickName avatarUrl')
.exec();
}
}
module.exports = new TranslationRequestService();
(如何使用处理程序)
// following lines are executed twice by user's repeated action on UI
var service = require('./translation-request-service);
service.getTranslationResultById(id);
答案 0 :(得分:0)
我通过立即克隆返回的结果并运行我的逻辑来修复问题。我仍然不知道是什么导致了这个问题。我所做的就是将填充结果发送到我的Web套接字到客户端。