2017-02-06 5 views
1

두 모델 CompanyCompanyAdmin이 있습니다. 모델 회사에는 많은 CompanyAdmin이 있습니다. bookshelf 플러그인 bookshelf-cascade-delete을 사용하여 모회사가 삭제되면 CompanyAdmins를 삭제하려고합니다. 나는 또한 knex을 사용하여 mysql db에 연결합니다. 여기

내 모델입니다 :Bookshelf cascade delete

const db = bookshelf(connection); 
db.plugin(cascadeDelete); 

const Company = db.Model.extend({ 
    tableName: 'company', 
    idAttribute: 'id', 
    hasTimestamps: true, 
    company_admins: function() { return this.hasMany(CompanyAdmin); }, // console.log(this); 
}, { 
    dependents: ['company_admins'], 
}); 

const CompanyAdmin = db.Model.extend({ 
    tableName: 'company_admin', 
    idAttribute: 'id', 
    hasTimestamps: true, 
    company: function() { return this.belongsTo(Company) }, 
}); 

나는 을 console.log (이)에서 company_admins이 기능은,이 데이터를 얻을 때 : 여기

ModelBase { 
    tableName: 'company', 
    idAttribute: 'id', 
    hasTimestamps: true, 
    company_admins: [Function: company_admins] } 

내 DELETE 경로 처리기 :

.delete((req, res) => { 
    if (req.user) { 
     Company.forge({ 
      id: req.user.attributes.company_id, 
     }) 
     .destroy() 
     .then(() => { 
      res.status(200) 
      .json({ 
       status: 200, 
       error: false, 
      }); 
      req.logout(); 
     }).catch((err) => { 
      console.log(err); 
      res.status(500) 
      .json({ 
       status: 500, 
       error: err.message, 
      }); 
     }); 
    } else { 
     res.status(401) 
     .json({ 
      status: 401, 
      message: 'User not authenticated', 
     }); 
    } 
}); 

이 오류가 발생합니다 :

TypeError: Cannot read property 'hasMany' of undefined

누구나 같은 문제가 있습니까?

답변

0

이 문제가 해결되었습니다. 서가를 가져 오는 중에 오류가 발생했습니다.

import bookshelf from 'bookshelf'; 
const db = bookshelf(connection); 

그리고이 같은 책장 모양의 정확한 수입 : 내가 전에 사용 된 코드는했다

const db = require('bookshelf')(connection); 

확실히 이전 코드가 작동하지 않았는지 모르겠지만, 수정 된 버전이 작동하지 않음 잘!