繁体   English   中英

Javascript 多次出现变量问题

[英]Javascript Variable issue hitting multiple times

 let data;

     if(post.images.some(img => img._id == doc_id)) {
        post.images.forEach(function(e,i){
          if (post.images[i]._id == doc_id) { 
             data = ..... // if i defined here const data then i know its block scope will be access within this block only
            let isData = false;
            if (data.length === post.images[i].data.length) {
              data.push(myId);
              isData = true;
            }
          post.images[i].data = data

          }
          })
     } else {
       data = ..... // if i defined here const data then i know its block scope will be access within this block only        let isData = false;
        if (data.length === post.data.length) {
          data.push(myId);
          isData = true;
        }
        post.data = data;

     }

     res.out(data)

获取错误错误 [ERR_HTTP_HEADERS_SENT]:无法在将标头发送到客户端后设置标头我知道它从我尝试使用 return 关键字修复的循环中出现问题,但没有成功。

我该如何解决此警告问题是否有任何其他方法可以解决此问题请协助

您应该尝试返回数据;

if(post.images.some(img => img._id == doc_id)) {
            post.images.forEach(function(e,i){
              if (post.images[i]._id == doc_id) { 
                 data = ..... // if i defined here const data then i know its block scope will be access within this block only
                  let isData = false;
                  if (data.length === post.images[i].data.length) {
                    data.push(myId);
                    isData = true;
                  }
                 post.images[i].data = data
                 return data;
               } 
            })
         }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM