2016-07-18 86 views
0
var UserSchema = new Schema({ 
    job : [{ 
    type : mongoose.Schema.Types.ObjectId, 
    experience : String, 
    grade : String, 
    ref: 'Company'}] 
}); 

用戶可以有多個作業。當我添加作業到用戶,我這樣做:引用在貓鼬的另一個架構使用額外字段

如果我有:

req.body.job == {type : company._id, experience : "bla bla", grade : "smth"} 

function addJob(req, res, next) { 

    var userId = req.user._id; 
    var job = req.body.job; 
    return User.findById(userId).exec() 
    .then(user => { 
     user.job.push(job); 
     return user.save() 
     .then(handleEntityNotFound(res)) 
     .then(respondWithResult(res)) 
     .catch(handleError(res)); 
    }); 
} 

我得到了一個錯誤:

Unhandled rejection CastError: Cast to ObjectId failed for value "[object Object]" at path "job" 

但如果我這樣做:

req.body.job == {type : company._id, experience : "bla bla", grade : "smth"} 

function addJob(req, res, next) { 

     var userId = req.user._id; 
     var job = req.body.job; 
     return User.findById(userId).exec() 
     .then(user => { 
      user.job.push(job.type); // <---------- 
      return user.save() 
      .then(handleEntityNotFound(res)) 
      .then(respondWithResult(res)) 
      .catch(handleError(res)); 
     }); 
    } 

它的工作原理,但experiencegrade是不確定的。我如何指定這些字段?


編輯:

因此,我改變UserSchema這樣的:

var UserSchema = new Schema({ 
    job : [{ 
    _company : { 
     type : mongoose.Schema.Types.ObjectId, 
     ref: 'Company' 
    }, 
    experience : String, 
    grade : String 
    }] 
}); 

而且我想:

//req.body.job == {_company:{type : company._id}, experience : "bla bla", grade : "smth"} 

function addJob(req, res, next) { 

    var userId = req.user._id; 
    var job = req.body.job; 
    return User.findById(userId).exec() 
    .then(user => { 
     user.job.push(job); 
     return user.save() 
     .then(handleEntityNotFound(res)) 
     .then(respondWithResult(res)) 
     .catch(handleError(res)); 
    }); 
} 

我得到了一個500(內部服務器錯誤)錯誤fr OM服務器

//req.body.job == {_company:{type : company._id}, experience : "bla bla", grade : "smth"} 

function addJob(req, res, next) { 

    var userId = req.user._id; 
    var job = req.body.job; 
    return User.findById(userId).exec() 
    .then(user => { 
     user.job.push(job._company); //<----------------- 
     return user.save() 
     .then(handleEntityNotFound(res)) 
     .then(respondWithResult(res)) 
     .catch(handleError(res)); 
    }); 
} 

而不會出現錯誤。但是仍然有任何關於grade和MongoDB中experience場...

回答

1

字段「類型」是貓鼬保留,然後

{ 
    type : mongoose.Schema.Types.ObjectId, 
    experience : String, 
    grade : String, 
    ref: 'Company' 
} 

在描述與類型的ObjectId的實體,引用公司(這個與.populate一起使用)。不幸的是,由於它作爲ObjectId保存在MongoDB中,其他字段將被忽略。

然後,您可以選擇引用該公司的領域之一,並能夠與像JobSchema檢索信息的額外位:

{ 
    _company : {type: mongoose.Schema.Types.ObjectId, ref: 'Company'} 
    experience : String, 
    grade : String 
} 

您將能夠填充的_公司領域你的工作又不失「體驗」和「等級」信息


您的版本之後:

在給定的JobSchema:

{ 
    _company : {type: mongoose.Schema.Types.ObjectId, ref: 'Company'} 
    experience : String, 
    grade : String 
} 

您的工作實例應該看起來像{_company:ObjectId(「.......」),experience:「blabla」,grade:「smth」}。然後你的req.body.job應該是{_company:company._id,experience:「bla bla」,grade:「smth」}

在你的第一個addJob中,你有一個500錯誤,因爲你要求MongoDB進行轉換一個看起來像{type:ObjectId(「....」)}的文檔到一個ObjectId中。

第二個addJob不會觸發錯誤,因爲您正在上傳類似於{type:ObjectId(「...」)}的文檔(並且順便失去了成績和體驗字段),而且您沒有模式字段是必需的,所以貓鼬會忽略您的字段類型並將空對象上傳到您的集合中。

TL;博士: 它應該是:

// req.body.job == {_company: company._id, experience : "bla bla", grade : "smth"} 

function addJob(req, res, next) { 
    var userId = req.user._id, 
     job = req.body.job; 

    return User.findById(userId) 
       .exec() 
       .then(
        user => { 
         user.job.push(job); 
         return user.save() 
            .then(handleEntityNotFound(res)) 
            .then(respondWithResult(res)) 
            .catch(handleError(res)); 
        } 
       ); 
} 
+0

是的,我想是這樣的。所以我想沒有其他的解決方案,對吧? – Emidomh

+0

@Emidomh我不認爲有另一種解決方案,對不起:/ – GnsBeldaran

+0

我編輯我的文章,請看看:) – Emidomh