2017-09-20 86 views
0
const KeyContactsPersonsSchema = new Schema({ 
    name: { 
    required: true, 
    type: String 
    }, 
    contactNumber: { 
    required: true, 
    type: String 
    }, 
    email: { 
    required: true, 
    type: String 
    } 
}); 

上述定義的模式已被列入以下模式keyContactsPersons屬性貓鼬陣列沒有父文檔中保存

const CompanySchema = new Schema({ 
    name: { 
    type: String, 
    required: true, 
    unique: true, 
    trim: true 
    }, 
    address: { 
    type: String, 
    required: true, 
    trim: true 
    }, 
    contactNumber: { 
    type: String, 
    required: true, 
    trim: true 
    }, 
    logo: { 
    type: String, 
    required: true, 
    trim: true 
    }, 
    keyContactsPersons: [KeyContactsPersonsSchema], 
    billingDetails: { 
    type: String, 
    required: true, 
    trim: true 
    }, 
    assignedEmployee: { 
    type: Schema.Types.ObjectId, 
    required: true, 
    trim: true 
    }, 
    status: { 
    type: Boolean, 
    default: false, 
    required: true, 
    trim: true 
    } 
}, { 
    timestamps: true 
}); 

最後保存傳遞給CompanyModel模型 JSON目的是like.while我試着去挽救這一點,讓我像

keyContactsPersons.0.email一個錯誤:路徑email是必需的, keyContact sPersons.0.contactNumber:路徑contactNumber是必需的。, keyContactsPersons.0.name:路徑name是必需的。

"company":{ 
     "name":"Leafy Code", 
     "address":"Hokandara", 
     "contactNumber":"075227785", 
     "logo":"/logo", 
     "billingDetails":"billingDetails", 
     "assignedEmployee":"5997e71ab6a13500018106b9", 
     "status": true, 
     "keyContactsPersons": [ 
      { 
       "name":"demo person", 
       "contactNumber":"0771568952", 
       "email":"[email protected]" 
      } 
     ] 
    } 


const newCompany = new CompanyModel(company); 
newCompany.save(); 
+0

是'你給company'變量'CompanyModel'等於'「公司」:... 'like:'company = {「company」:...}'? –

回答

0

我從代碼假設包含在你的問題,該公司可變這裏傳遞,CompanyModel(公司)包含對象{ 「公司」:{...}}

你的架構沒有一個公司財產,所以對象應該是這樣的:

const company = { 
    "name": "Leafy Code", 
    "address": "Hokandara", 
    "contactNumber": "075227785", 
    "logo": "/logo", 
    "billingDetails": "billingDetails", 
    "assignedEmployee": "5997e71ab6a13500018106b9", 
    "status": true, 
    "keyContactsPersons": [{ 
    "name": "demo person", 
    "contactNumber": "0771568952", 
    "email": "[email protected]" 
    }] 
} 

const newCompany = new CompanyModel(company) 
newCompany.save()