2016-11-03 15 views
1

ロールのプロパティの変更をプロファイルから試すときに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; 
     }); 
    }; 
} 
+0

「findByIdAsync(...)」catch((err)=> {console.log(err); handleError(res);}) ' –

+0

reqには何があるのか​​教えてもらえますか? params.id'正確にどうぞ?コンソールのログ(\ 'REQUEST PARAM ID:$ {req.params.id} \'); ' –

+0

@GrégoryNEUTによって表示されるものです。console.logにparams.idを書きます。 : 'REQUESTのPARAM ID:57bd9db4f33dcd03c3fd9990' –

答えて

0

教訓:ドキュメントを正しく読んでください。

私はこのアプリケーションにブルーバードの約束を使用しているので、私はsaveUpdates()コールバック関数内で.spread()を使うのを忘れていました。

ソリューション:

function saveUpdates(updates) { 
    return function(entity) { 
    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() 
     // use `.spread()` and not `.then()` // 
     .spread(updated => { 
     return updated; 
     }); 
    }; 
} 

私はこの結論に至っ次SOA感謝したい:http://bluebirdjs.com/docs/api/spread.htmlhttps://stackoverflow.com/a/25799076/5994486

はまた、ここでは誰もが.spread()に興味があった場合、青い鳥のドキュメントへのリンクです

関連する問題