2017-01-18 1 views
0

나는 현재 SchoolsContacts의 두 가지 컬렉션을 가지고 있습니다. Schools 컬렉션에는 이미 수천 개의 데이터가 채워져 있으며 Contacts은 아직 비어있는 새 컬렉션입니다.많은 관계로 데이터를 만드는 돛

이제는 두 가지 모두 스키마가 변경되었습니다. 다음은 스키마 세부의 : 새 연락처를 만들 때 여기

// ----------- Schools.js 
module.exports = { 
    attributes: { 
    user: { 
     model: 'users' 
     // Của User đã tạo 
    }, 

    essential: { 
     type: 'string' 
     // Lưu toàn bộ thông tin của essential 
    }, 

    contacts: { 
     collection: 'contacts', 
     via: 'schools' 
    }, 

    pipeline: { 
     type: 'string', 
     defaultTo: "" 
    }, 

    status: { 
     type: 'integer', 
     defaultsTo: 1 
     // 0: un-active, 1: active 
    } 
    } 
}; 

// ----------- Contacts.js 

module.exports = { 
    attributes: { 
     title: { 
      type: 'string' 
     }, 

     firstName: { 
      type: 'string' 
     }, 

     lastName: { 
      type: 'string' 
     }, 

     email: { 
      type: 'string' 
     }, 

     position: { 
      type: 'string' 
     }, 

     landlinePhone: { 
      type: 'string' 
     }, 

     mobilePhone: { 
      type: 'string' 
     }, 

     externalCompany: { 
      type: 'string' 
     }, 

     schools : { 
      collection: 'schools', 
      via: 'contacts', 
      dominant: true 
     }, 

     user: { 
      model: 'users' 
     }, 

     activities: { 
      collection: 'activities', 
      via: 'contact' 
     }, 

     status: { 
      type: 'integer', 
      defaultsTo: 1 
      // 0: remove, 1: active, 2: unactive 
     } 
    } 
}; 

을 그리고 것은 내 코드입니다 : 그러나

Contacts.create(contactData).exec(function (e1, newContact) { 
    if (e1) { 
     console.log("e1 ::: ", e1); 
    }; 

    // newContact is filled with contact new data.. 
    console.log("Contact data has been created", newContact, schoolId); 

    // Adding schools to newContact (schoolId has a value here); 
    newContact.schools.add(schoolId); 

    // Save 
    newContact.save(function (err) { console.log('err', err) }); 
}); 

, 내 데이터베이스에 검사 할 때, contacts은 만들었지 만 schools 속성없이되었습니다. 그리고 관련 schools에는 여전히 contacts 속성이 없습니다.

나는 다음을 시도했다.

newContact.schools.add([schoolId]); 
// or -- 
newContact.schools.add(schoolObj); 
// or -- 
newContact.schools.add([schoolObj]); 

나는 두 모음 사이의 dominant 속성을 전환하는 시도하고 실행 내가 이러한 솔루션없이 lucks을 시도했습니다

school.contacts.add(newContact); 

, 같은 반전하고;

  • 내가 여기서 뭔가를 놓치고 있습니까? 모든 제안은 감사하겠습니다.

  • 답변

    1

    코드는 괜찮습니다. 을 contacts에 연결합니다.

    sails console,이 실행 :

    Contacts.find(contactId).populateAll().exec(console.log); 
    

    그것을 채워 학교 배열과 함께 접촉을 인쇄하는 경우, 데이터는 DB에 유지됩니다. MongoDB의 Contacts 안에있는 것이 아니라 별도의 콜렉션에있을 수 있습니다.

    PS : pipeline 정의 (defaultsTo을해야한다) 내가 너무 바보 .. 난 그냥 많은 관계로 많은 다른 컬렉션에 있음을 실현하고

    +0

    defaultTo에 오타가 있습니다. 귀하의 답변 주셔서 감사합니다 .. –

    관련 문제