我尝试在firebase上更新这个文档,这是第一次工作,但是在那之后,它给了我这个错误:
Error: Can't set headers after they are sent这是切换值的按钮:
getActionButtons = () => {
if(this.state.stepIndex > 2){
return (<ActionButtons>
<ActionButton label={this.state.userType === 'Rider' ? this.state.available ? 'Found a carpool?' : 'look for carpool' : this.state.available ? 'Car is Full?' : 'Available for carpool'} primary={!this.state.available} secondary={this.state.available} onClick={() => {
console.log("eventId: ", this.props.eventId.event)
console.log("userId: ", firebase.auth().currentUser.uid)
// firebase.database().ref('/' + this.props.eventId.event + '/posts/' + firebase.auth().currentUser.uid).transaction((post => {
// if (post) {
// if (post.available) {
// post.available = !post.available
// } else {
// post.available = this.state.available
// }
// }
// return post;
// }))
firebase.database().ref('/' + this.props.eventId.event + '/posts/' + firebase.auth().currentUser.uid + '/available').set(!this.state.available)
this.setState({
available: !this.state.available
});
this.props.update();
}}/>
</ActionButtons>)
}
}服务器代码:
const activeRoute = routes.find(route => matchPath(req.url, route)) || {}
const isEventPage = activeRoute.name === "carpoolingPage"
app.get("*", (req, res, next) => {
if(isEventPage) {
if (req.params[0]) {
let eventRef = firebase.database().ref(req.params[0])
eventRef.on("value", (snapshot) => {
const event = snapshot.val()
// console.log("event: ", event)
if(!event)
res.redirect("/eventnotfound")
else {
const { title, location, description } = event
const preloadedState = { currentEvent: event }
const store = configureStore(preloadedState)
const markup = generateMarkup(store)
res.send(HTML({
title: `Carpooling to ${ title }`,
markup, styleTags,
preloadedState,
ogTitle: `Carpooling to ${ title }`,
ogDescription: `Find people to carpool with to ${ title }. Choose to ride or drive to ${ location } ${ description === null ? '' : `Event Description: ${ description }`}`
}))
}
})
}
}
else {
const store = configureStore()
const markup = generateMarkup(store)
res.send(HTML({ markup, styleTags }))
}
})我在Node和React中使用SSR,而且快递服务器只使用cors()。
FYI,它在firebase上正确地切换值--服务器在上面的错误中退出的问题,这会导致后续代码无法运行。
顺便说一句,我尝试过使用firebase事务,并试图在firebase行之后删除代码,但是它会产生相同的错误。
我跟踪了堆栈溢出的所有类似问题,但这与我的问题不匹配。
发布于 2018-07-10 13:02:44
我确信eventRef.on("value", ...)在这一次发生的次数更多。您的res.redirect或res.send被多次调用。直截了当地说,如果你回复了一次,你就不能再回复了。希望这能解决你的问题。
让evetRef.on到eventRef.once。这会解决你的问题。
https://stackoverflow.com/questions/51265230
复制相似问题