Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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/Express:TypeError:res.json不是函数_Javascript_Node.js_Express_Error Handling_Server - Fatal编程技术网

JavaScript/Express:TypeError:res.json不是函数

JavaScript/Express:TypeError:res.json不是函数,javascript,node.js,express,error-handling,server,Javascript,Node.js,Express,Error Handling,Server,我的前端已成功连接到我的服务器;但是,我的初始提取请求抛出了一个: GET http://localhost:8080/events net::ERR_EMPTY_RESPONSE 每次执行操作时,我的服务器都会抛出: UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: res.json is not a function 这是我的server.js: // R

我的前端已成功连接到我的服务器;但是,我的初始提取请求抛出了一个:

GET http://localhost:8080/events net::ERR_EMPTY_RESPONSE
每次执行操作时,我的服务器都会抛出:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: res.json is not a function
这是我的server.js:

// Require and initiate Express
const express = require('express');
const app = express();

//Import controllers, db and bodyParser
const controller = require('./controllers');
const bodyParser = require('body-parser');
const cors = require('cors');
require('./db');


//Add bodyParser & cors middleware
app.use(cors());
app.use(bodyParser.json());

//Define routes for List Events, Add Event, and Delete Event
app.get('/events', controller.listEvents);
app.post('/events', controller.addEvent);
app.delete('/events/:id', controller.deleteEvent);


//Initiate server on port 8080
app.listen(8080, () => {
  console.log('Server is listening on http://localhost:8080');
})
下面是进入my controller.js的步骤:

//Gets list of events
exports.listEvents = (req, res) => {
  Event.find()
  .then(events => {
    res.json(events);
  })
}
任何建议都会被广泛接受

Event.find承诺失败。 没有捕获块来捕获错误。这就是未处理PromisejectionWarning的原因

您可以用如下错误进行响应:

exports.listEvents = (req, res, next) => {
  Event.find()
  .then(events => {
    res.json(events);
  }).catch(err => { console.log(err); next(err) });
}
app.use((err, req, res, next) => {
  res.status(err.status || 500).json({
    message: err.message || "Something went wrong. Please try again",
    status: err.status || 500
  });
});
并在server.js中添加错误处理程序,如下所示:

exports.listEvents = (req, res, next) => {
  Event.find()
  .then(events => {
    res.json(events);
  }).catch(err => { console.log(err); next(err) });
}
app.use((err, req, res, next) => {
  res.status(err.status || 500).json({
    message: err.message || "Something went wrong. Please try again",
    status: err.status || 500
  });
});

这不会解决您的问题,只会告诉您问题所在。

Event.find promise失败了。没有捕获块来捕获错误。这就是PromiserEjectionWarninghi Faizuddin未处理的原因,我已经添加了捕获,它揭示了我的问题实际上在前端。谢谢