ロールのプロパティの変更をプロファイルから試すときに500のエラーが発生するため、スキーマを正しく設計したかどうか判断できません。 (注:これは実際に有益ではないので、500エラーは単に、{}空で応答)以下Mongooseサブ文書が更新されません
プロファイルスキーマである:
var ProfileSchema = new Schema({
name: {
type: String,
required: true
},
roles: [{
application: {
type: Schema.Types.ObjectId,
required: true,
ref: 'Application'
},
role: {
type: String,
required: true,
enum: [ 'admin', 'author', 'publisher' ]
}
}]
});
各プロファイルは、アプリケーションのための役割を有し、そして場合私はそれが失敗し、コントローラのアクション「更新」にリクエストを送信します。
プロファイル更新コントローラ:
// Updates an existing Profile in the DB
export function update(req, res) {
try {
if (req.body._id) {
delete req.body._id;
}
console.log('ENDPOINT HIT...');
console.log(`REQUEST PARAM ID: ${req.params.id}`);
console.log('REQUEST BODY:');
console.log(req.body);
console.log('ENTIRE REQUEST: ');
return Profile.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(saveUpdates(req.body))
.then(respondWithResult(res))
.catch(handleError(res));
} catch(ex) {
console.error('FAILED TO UPDATE PROFILE');
return handleError(res);
}
}
私はidと体が正常に送信されたことを確認しました、私は終点に向かっています。
これは、リクエストボディJSONの例である:私はIDによるプロフィールを見つけようとすると
{
_id: 57e58ad2781fd340563e29ff,
__updated: Thu Oct 27 2016 15:41:12 GMT-0400 (EDT),
__created: Fri Sep 23 2016 16:04:34 GMT-0400 (EDT),
name: 'test',
__v: 11,
roles:[
{ application: 57b70937c4b9fe460a235375,
role: 'admin',
_id: 58125858a36bd76d8111ba16 },
{ application: 581b299f0145b48adf8f57bd,
role: 'publisher',
_id: 581b481898eefb19ed8a73ee }
]
}
、約束チェーンはキャッチ(handleErrorの(RES))に直進します。コードの一部であり、私のコンソールに空のオブジェクトを表示します。
:私のハンドル誤差関数:それは私のsaveUpdates機能(:私はlodashを使用しています注)に当たったとき、私は、コードを実現しています
function handleError(res, statusCode) {
console.error('HANDLE PROFILE ERROR: ', statusCode);
statusCode = statusCode || 500;
return function(err) {
console.error('PROFILE ERROR:');
console.error(JSON.stringify(err, null, 2));
res.status(statusCode).send(err);
};
}
UPDATE
が破壊されます
function saveUpdates(updates) {
/// the code is fine here ///
return function(entity) {
/// once it enters in here, is where it begins to break ///
var updated = _.merge(entity, updates);
if(updated.roles.length != updates.roles.length) {
updated.roles = updates.roles;
}
for(var i in updates.roles) {
updated.roles.set(i, updates.roles[i]);
}
return updated.saveAsync()
.then(updated => {
return updated;
});
};
}
「findByIdAsync(...)」catch((err)=> {console.log(err); handleError(res);}) ' –
reqには何があるのか教えてもらえますか? params.id'正確にどうぞ?コンソールのログ(\ 'REQUEST PARAM ID:$ {req.params.id} \'); ' –
@GrégoryNEUTによって表示されるものです。console.logにparams.idを書きます。 : 'REQUESTのPARAM ID:57bd9db4f33dcd03c3fd9990' –