2017-01-18 1 views
0

Ich habe derzeit zwei Sammlungen, die Schools und Contacts sind. Die Schools-Sammlung wurde bereits mit Tausenden von Daten gefüllt, während Contacts eine neue Sammlung ist, die noch leer ist.Segel erstellen Daten in viele zu viele Beziehung

Jetzt gibt es eine Schemaänderung für beide. Hier ist das Schema Detail:

// ----------- 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 
     } 
    } 
}; 

Und mein Code ist hier, wenn Sie einen neuen Kontakt erstellen:

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) }); 
}); 

Allerdings, wenn ich auf meine Datenbank überprüfen, contacts wurde erstellt, aber ohne schools Attribute. Und das verwandte schools hat auch noch kein contacts Attribut.

Ich habe Folgendes versucht;

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

Ich habe versucht, das dominant Attribut zwischen diesen beiden Sammlungen zu wechseln und wie die Ausführung umgekehrt Tun

school.contacts.add(newContact); 

Ich habe auch diese Lösung und keine lucks versucht;

Fehle ich etwas hier? Jeder Vorschlag wird geschätzt ..

Antwort

1

Ihr Code ist in Ordnung, es verbindet schools zu contacts.

In sails console, dies auszuführen:

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

Wenn es Kontakt druckt zusammen mit Schulen Array gefüllt, werden die Daten in DB anhielt. Es könnte in einer separaten Sammlung sein, anstatt innerhalb von Contacts in MongoDB zu sein.

PS: pipeline Definition hat einen Tippfehler in defaultTo (sollte defaultsTo sein)

+0

ich so dumm bin .. Ich habe erkannt, dass die viele zu viele Beziehung in anderen Sammlungen. Danke für deine Antwort.. –