2017-02-02 29 views
1

我收到此錯誤信息下面HapiJS未予答覆,叫了兩聲

Unhandled rejection Error: reply interface called twice 

注意的片段,我使用return所有回覆()接口

Locations 
    .findOne({ 
     _id: request.params.id, 
     activationCode: payload.activationCode 
    }).then((location) => { 
     if (!location) { 
     return reply(Boom.notFound('Location not found')) 
     } 
     locationObject = location 
     if (payload.password !== payload.confirmPassword) { 
     return reply(Boom.badRequest('Password and Confirm Password must match')) 
     } 
     if (!payload.terms) { 
     return reply(Boom.badRequest('Must agree to Terms & Conditions')) 
     } 
     return newPaymentMethod.save() 
    }).then((paymentMethod) => { 
     ..... 
     return user.save() // user is defined at ..... 
    }).then(() => { 
     return reply({ token: helpers.createJwt(userObject) }) 
    }).catch((err) => { 
     return reply(Boom.wrap(err)) 
    }) 

任何幫助將不勝感激。

回答

2

由於不正確地使用了承諾,您好像陷入了這種情況。我想你正在執行你的代碼片段,你可以訪問reply

正如你return承諾鏈中的荷蘭國際集團的迴應,你倆return值到下一個.then(承諾),也叫從外部範圍的reply

我建議你使用承諾reject的錯誤,所以你只需要在承諾的.catch()內的一個reply(Boom.method())

0

既然你到底

.then(() => { 
    return reply({ token: helpers.createJwt(userObject) }) 
}).catch((err) => { 
    return reply(Boom.wrap(err)) 
}) 

你可以稱之爲reply兩次,如果任何的if條件爲真連鎖的承諾。 簡單的解決辦法是在if條件成立時發生錯誤 - 因爲catch塊中已經有一個Boom.wrap