首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:无法设置标头发送后,SSR反应+节点

错误:无法设置标头发送后,SSR反应+节点
EN

Stack Overflow用户
提问于 2018-07-10 12:28:21
回答 1查看 125关注 0票数 0

我尝试在firebase上更新这个文档,这是第一次工作,但是在那之后,它给了我这个错误:

代码语言:javascript
运行
复制
Error: Can't set headers after they are sent

这是切换值的按钮:

代码语言:javascript
运行
复制
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>)
    }
  }

服务器代码:

代码语言:javascript
运行
复制
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行之后删除代码,但是它会产生相同的错误。

我跟踪了堆栈溢出的所有类似问题,但这与我的问题不匹配。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-10 13:02:44

我确信eventRef.on("value", ...)在这一次发生的次数更多。您的res.redirectres.send被多次调用。直截了当地说,如果你回复了一次,你就不能再回复了。希望这能解决你的问题。

evetRef.oneventRef.once。这会解决你的问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51265230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档