2016-11-03 6 views
1

프로필에서 역할 속성을 변경하려고 시도 할 때 500 오류가 발생했기 때문에 스키마를 올바르게 디자인했는지 파악하는 데 문제가 있습니다. 다음은 몽구스 하위 문서가 업데이트되지 않습니다

프로파일 스키마입니다 : (500 오류가 단지} {빈 응답, 그래서 정말 유익하지 않다 주) :

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' ] 
    } 
    }] 
}); 

각 프로파일은 응용 프로그램에 대한 역할을, 때 나는 ID와 몸이 제대로 전송되는 것을 확인했다

// 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); 
    } 
} 

:

프로필 업데이트 컨트롤러 : 나는 실패, 컨트롤러 액션 '갱신'에 요청을 보내 나는 끝점을 치고있다.

는 요청 본문의 JSON의 예입니다

{ 
    _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 } 
    ] 

} 

내가 ID로 프로필을 찾아보십시오, 약속 체인 캐치 (는 handleError (고해상도))로 바로 간다; 코드의 일부이며 내 콘솔에 빈 개체를 표시합니다.

내 핸들 오류 기능 :

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

나는 내 saveUpdates 기능 ( : 내가 사용하고 lodash) : 칠 때 코드가 파괴되어 실현하고

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' 정확히 제발? console.log (\'REQUEST PARAM ID : $ {req.params.id} \');' –

+0

@ GrégoryNEUT, console.log에 params.id를 표시 할 때 표시되는 내용입니다. : '요청 PARAM ID : 57bd9db4f33dcd03c3fd9990' –

답변

0

교훈 : 문서를 올바르게 읽으십시오.

이 응용 프로그램에 블루 버드 약속을 사용하고 있으므로 saveUpdates() 콜백 기능에서 .spread()을 사용하는 것을 잊었습니다.

솔루션 :

나는이 결론을 이끌어 다음 SOA 감사드립니다
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; 
     }); 
    }; 
} 

: 또한 https://stackoverflow.com/a/25799076/5994486

을, 여기 .spread()에 호기심이 경우 누구의 블루 버드 문서에 링크 된입니다 : http://bluebirdjs.com/docs/api/spread.html

관련 문제