2017-01-23 61 views
0

我有一個應用程序,我試圖將聯繫人記錄添加到用戶「個人檔案」文檔,其中每個記錄都有多個電話號碼和電子郵件地址的數組/子文檔。更新子文檔不起作用

所以配置文件架構是這樣的:

var ContactSchema = require('./contact'); 
var UserSchema = new Schema({ 

    first_name:{ 
     type: String 
    }, 
    last_name:{ 
     type: String 
    }, 
    contacts:[ContactSchema] 
}); 

的ContactSchema看起來是這樣的:

var ContactSchema = new Schema({ 

    contact_name:{ 
     type: String 
    }, 
    contact_age:{ 
     type: String 
    }, 
    phones:[{ 
     phone_number:{type: String}, 
     phone_type:{type: String} 
    }], 
    emails:[{ 
     email_address:{type: String}, 
     email_type:{type: String} 
    }] 
}); 

我上面簡單的模式,但它們基本上代表了什麼我處理的結構用。

我的挑戰是在ExpressJs/Mongoose API中,我想傳遞對象並更新子文檔。

這裏是我認爲它會做,但是這並不工作:

var express = require('express'); 
var router = express.Router(); 
var Profile = require('../models/profile'); 
var vCard = require('vcards-js'); 

router.route('/addcontact/:ownerId') 

.put(function(req, res){ 

    Profile.findOne({'owner_id':req.params.ownerId}, function(err, profile){ 

    if(err) 
     res.send(err); 

    profile.contacts.push({ 
     first_name : req.body.first_name, 
     last_name : req.body.last_name 
    }) 

    req.body.phones.forEach(function(phone, index, arr){ 
     profile.contacts.phones.push({ 
     phone_type:phone.phone_type, 
     phone_number:phone.phone_number 
     }) 
    }) 

    req.body.emails.forEach(function(email, index, arr){ 
     profile.contacts.emails.push({ 
     email_type:email.email_type, 
     email_address:email.email_address 
     }) 
    }) 

    profile.save(function(err){ 
     if(err) 
     res.send(err); 
     res.json(profile); 
    }) 

    }); 
}); 


module.exports = router; 

所以要澄清一下,我有一個現有的「用戶配置文件」的紀錄。 用戶架構有一個「contacts」數組來保存多個聯繫人。 每個聯繫人都有電話和電子郵件陣列,因此每個聯繫人記錄可以保存多個電子郵件和電話號碼。

我想要做的是.PUT聯繫人記錄和更新用戶配置文件文件。

感謝任何聖人的忠告!

回答

1

您的User模型似乎沒有owner_id。假設它在這裏被遺漏,並且你想添加一個新的聯繫人給現有的用戶。

'use strict'; 

var express = require('express'); 
var router = express.Router(); 
var Profile = require('../models/profile'); 

router.route('/addcontact/:ownerId').put(function (req, res) { 
    var phones = req.body.phones; 
    console.log('phones', phones); 

    var emails = req.body.emails; 
    console.log('emails', emails); 

    var contacts = { 
    contact_name: req.body.first_name, 
    contact_age: '25', 
    emails: emails, 
    phones: phones 
    }; 

    Profile.findOneAndUpdate({ 
    owner_id: req.params.ownerId 
    }, { 
    $push: { 
     contacts: contacts 
    } 
    }, { 
    new: true 
    }, (err, profile) => { 
    if (err) { 
     return res.send(err); 
    } 
    return res.json(profile); 
    }); 

}); 


module.exports = router; 

而且,發送響應時避免代碼的進一步執行我對代碼進行一個微小的變化是增加return

0
var phonesArray = req.body.phones; 
var emailsArray = req.body.emails; 

Profile.findOneAndUpdate(
{'owner_id':req.params.ownerId} 
{phones: {$push: {$each: phonesArray}}, emails: {$push: {$each: emailsArray }}}, function(err, profile){ 
    if(err) 
     res.send(err); 

    console.log(profile); 
} 
+0

嗨,傑克遜,試過你的代碼,但我只是空白數組。我錯過了什麼嗎?我可以console.log並看到數組正在傳遞... – cnak2

+0

空數組是phoneArray和emailsArray?或更新後的數據庫? – Jackson

+0

請寫下你的代碼,用console.log和這裏的日誌結果。 – Jackson