Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试加载根路径时,应用程序返回错误_Javascript_Node.js_Express - Fatal编程技术网

Javascript 尝试加载根路径时,应用程序返回错误

Javascript 尝试加载根路径时,应用程序返回错误,javascript,node.js,express,Javascript,Node.js,Express,我编写了一个简单的javascript文件,允许我通过web浏览器查看数据库中的值。它以前工作过,但现在返回以下错误: _http_outgoing.js:494 throw new Error('Can\'t set headers after they are sent.'); ^ Error: Can't set headers after they are sent. at validateHeader (_http_outgoing.js:494:11)

我编写了一个简单的javascript文件,允许我通过web浏览器查看数据库中的值。它以前工作过,但现在返回以下错误:

_http_outgoing.js:494
    throw new Error('Can\'t set headers after they are sent.');
    ^

Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:494:11)
    at ServerResponse.setHeader (_http_outgoing.js:501:3)
    at ServerResponse.header (issuesDB/node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (issuesDB/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (issuesDB/node_modules/express/lib/response.js:267:15)
    at ServerResponse.send (issuesDB/node_modules/express/lib/response.js:158:21)
    at db.all (issuesDB/app.js:23:21)
    at Statement.errBack (issuesDB/node_modules/sqlite3/lib/sqlite3.js:16:21)
我非常确定我没有改变代码或数据库中的任何内容,所以不确定为什么它现在失败了,任何帮助都将不胜感激

代码如下:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const morgan = require('morgan');
const sqlite = require('sqlite3');
const db = new sqlite.Database('./issues.sqlite');
const dbname = 'issueInventory';
const PORT = process.env.PORT || 4001

app.use(bodyParser.json());
app.use(morgan('dev'));


app.get('/', (req,res,next) => {
  db.all(`select * from $dbname;`,
    {
      $dbname: dbname
  },
  (err,rows) =>{
    if (err) {
      res.status(500).send(`Something went wrong, we are working on it`)
    }
    res.status(200).send({issues: rows})
  });
});


app.listen(PORT, () => {
  console.log(`Application Server listening on ${PORT}`)
});

module.exports = app;

这就像你返回了两个响应。您错过了
return
语句

if (err) return res.status(500).send('Something went wrong, we are working on it');
res.status(200).send({issues: rows});

这就像你返回了两个响应。您错过了
return
语句

if (err) return res.status(500).send('Something went wrong, we are working on it');
res.status(200).send({issues: rows});

事实上,现在它已恢复运行!我还删除了表名的占位符,猜测它也不喜欢那个部分。非常感谢您的帮助:)的确-现在它已经恢复运行了!我还删除了表名的占位符,猜测它也不喜欢那个部分。非常感谢您的帮助:)