2017-01-18 6 views
0

私は現在、SchoolsContactsの2つのコレクションを持っています。 Schoolsコレクションには既に1000個のデータが格納されていますが、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]); 

私はそれらの2つの収集の間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のタイプミスがあります。ご回答有難うございます.. –