2017-04-24 4 views
0

그룹 모델 내에 있습니다. 그룹 모델에서 프리 후크를 저장하면 정규식이 사용자 검색을 위해 업데이트됩니다. 하지만 몽구스는 "User.find는 함수가 아닙니다."라고 반환하고, console.logged User는 빈 Object ({})를 반환했습니다. 그룹 저장 프리 훅 내에서 사용자에게 쿼리 할 수없는 이유는 무엇입니까?몽구스 모델이 다른 모델 내부의 빈 개체입니다.

var User = require('./User') 
... 
    _schema.pre('save', function (next) { 
     this.regex = '' 
     if (!this.users) { return next() } 
     var userIds = this.users.map(s => s._id) 
     User.find({ _id: { $in: userIds } }).select('name surname').lean().then(docs => { 
      docs.forEach(d => { 
       this.regex += (' ' + d.name + ' ' + d.surname) 
      }) 
      next() 
     }) 
    }) 
+0

순환 종속성으로 인해 '사용자'모델이 호출하는 모델에 따라 달라질 수 있습니다. [more] (https://stackoverflow.com/questions/44816014/mongoose-model-is-an-empty-object-inside-post-hook-of-another-model/44817920#44817920) – NERDYLIZARD

답변

0

바로 이동 사용자 정의 내선이 작동합니다.

_schema.pre('save', function (next) { 
    var User = require('./User') 
     this.regex = '' 
     if (!this.users) { return next() } 
     var userIds = this.users.map(s => s._id) 
     User.find({ _id: { $in: userIds } }).select('name surname').lean().then(docs => { 
      docs.forEach(d => { 
       this.regex += (' ' + d.name + ' ' + d.surname) 
      }) 
      next() 
     }) 
    })