class Main extends React.Component{
constructor(props){
super(props);
this.titleHandler = this.titleHandler.bind(this);
this.sidebarHandler = this.sidebarHandler.bind(this);
this.state = {
pageTitle: '',
mounted: false,
data: [],
sidebar: false
}
}
// Title Handler
titleHandler(){
console.log('Page Title Function Called');
this.setState({ pageTitle: this.state.pageTitle });
}
// Sidebar Handler
sidebarHandler(){
console.log('Sidebar Function Called');
this.setState({ sidebar: !state.sidebar });
}
// Load Dashboard
loadDashboard(){
ReactDOM.render(<Dashboard/>, document.querySelector('#main-content'));
}
// Load New Page
loadNewPage(){
ReactDOM.render(<NewPage action={this.sidebarHandler} />, document.querySelector('#main-content'));
}
// Render
render(){
return(
<div id="wrapper" className="wrapper">
<button onClick={this.loadDashboard}>Load Dashboard</button>
<button onClick={this.loadNewPage}>Load New Page</button>
{/* Content: Start */}
<div id="content" className="content">
{/* Main: Start */}
<div id="main" className="main">
<div className="section-title">
<h2>{this.state.pageTitle}</h2>
</div>
{/* Main Content Container: Start */}
<div id="main-content" className="main-content container-fluid"></div>
{/* Main Content Container: End */}
</div>
{/* Main: End */}
{this.state.sidebar ? <Sidebar/> : null}
</div>
{/* Content: End */}
</div>
);
};
}
ReactDOM.render(<Main />,document.querySelector('#root'));
我故意未定义class NewPage extends React.Component{
constructor(props){
super(props);
this.state = {
pageTitle: 'New Page',
sidebar: false
};
this.props.action.bind(this);
}
// Component Will Mount
componentWillMount(){}
// Component Did Mount
componentDidMount(){}
// Component Will Unmount
componentWillUnmount(){}
// Render
render(){
return(
<div>
New Page Content...
</div>
);
}
}
,以查看返回的错误是否会传递给next(),但不会传递给它们。它只是给了我一个未处理的Promise拒绝错误,并且响应不会传递到下一个回调,该回调将发送状态为500的请求和错误消息。
exports.verifyReCAPTCHA = function(req, res, next) {
return new Promise((resolve, reject) => {
let recaptcha = new Recaptcha(process.env.RECAPTCHA_SITE_KEY, process.env.RECAPTCHA_SECRET_KEY, recaptchaData);
recaptcha.verify((success, error_code) => {
if (success) {
return resolve()
}
return reject(next(new Error(error_code)))
})
return reject(next(new Error('Unknown error occured!')))
})
}
答案 0 :(得分:0)
您的中间件使用情况不正确您不会从中间件返回Promise
,因为express
不会呼叫您的.then
。看起来您正在尝试链接中间件。这是您的操作方法:
exports.verifyReCAPTCHA = function(req, res, next) {
let recaptcha = new Recaptcha(process.env.RECAPTCHA_SITE_KEY, process.env.RECAPTCHA_SECRET_KEY, recaptchaData);
recaptcha.verify((success, error_code) => {
if (success) {
return next(); // <-- calling next(), go to next middleware
}
return next(new Error(error_code))
})
next(new Error('Unknown error occured!')) // <-- not sure where this is originating from, try..catch perhaps?
}