所以我的代码都工作正常,直到在我的一个应用程序控制器上添加了一些代码,现在当我尝试发布数据时,它引发了我这个错误:
(node:10928) UnhandledPromiseRejectionWarning: TypeError: Cannot set
property 'approved' of null
at store (C:\Users\sadkevin\Desktop\Programs\Rocketseat\SemanaOmnistack9\backend\src\controllers\RejectionController.js:9:26)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:10928) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:10928) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
这是我的RejectionController,我在其中设置预订是否为假(还有一个ApprovalController,它仅将钉书的假值更改为true,并抛出相同的错误):
const Booking = require('../models/Booking');
module.exports = {
async store(req, res) {
const { booking_id } = req.params;
const booking = await Booking.findById(booking_id).populate('spot');
booking.approved = false;
await booking.save();
return res.json(booking);
}
};
这是我的预定负责人:
const Booking = require('../models/Booking');
module.exports = {
async store(req, res) {
const { user_id } = req.headers;
const { spot_id } = req.params;
const { date } = req.body;
const booking = await Booking.create({
user: user_id,
spot: spot_id,
date,
});
await booking.populate('spot').populate('user').execPopulate();
const ownerSocket = req.connectedUsers[booking.spot.user];
if (ownerSocket) {
req.io.to(ownerSocket).emit('booking_request', booking);
}
return res.json(booking);
}
};
这是预订的MongoDB模式:
const mongoose = require('mongoose');
const BookingSchema = new mongoose.Schema({
date: String,
approved: Boolean,
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
spot: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Spot'
}
});
module.exports = mongoose.model('Booking', BookingSchema);
这些是我申请的路线:
const express = require('express');
const multer = require('multer');
const uploadConfig = require('./config/upload');
const SessionController = require('./controllers/SessionController');
const SpotController = require('./controllers/SpotController');
const DashboardController = require('./controllers/DashboardController');
const BookingController = require('./controllers/BookingController');
const ApprovalController = require('./controllers/ApprovalController');
const RejectionController = require('./controllers/RejectionController');
const routes = express.Router();
const upload = multer(uploadConfig);
routes.post('/sessions', SessionController.store);
routes.get('/spots', SpotController.index);
routes.post('/spots', upload.single('thumbnail'), SpotController.store);
routes.get('/dashboard', DashboardController.show);
routes.post('/spots/:spot_id/bookings', BookingController.store);
routes.post('/bookings/:booking_id/approvals', ApprovalController.store);
routes.post('/bookings/:booking_id/rejections', RejectionController.store);
module.exports = routes;
请帮助。
答案 0 :(得分:0)
很难说出错误的根本原因是什么,但是booking
变量从Promise返回后设置为null。在数据库中找不到预订,或者没有通过请求参数传递booking_id
。我建议您在try catch中包装您的异步等待代码,以帮助调试并查看所记录的控制台错误。
async store(req, res) {
try {
const { booking_id } = req.params;
const booking = await Booking.findById(booking_id).populate('spot');
booking.approved = false;
await booking.save();
return res.json(booking);
} catch (err) {
console.error(err);
}
}
答案 1 :(得分:0)
尝试将您的认可:布尔值更改为认可:布尔值? 在您的MongoDB模式中
这将使它可以为空
答案 2 :(得分:0)
发现问题所在,在Insomnia,我没有传递预订ID,这就是为什么它无法设置null属性的原因。