gpt4 book ai didi

javascript - 如何修复 "TypeError: Cannot set property ' 已批准“为空”?

转载 作者:太空宇宙 更新时间:2023-11-04 01:23:38 30 4
gpt4 key购买 nike

所以我的代码一切正常,直到在我的一个应用程序 Controller 上添加了一些代码,现在当我尝试发布数据时它抛出了这个错误:

(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,我在其中设置预订是否为 false(还有一个 ApprovalController 只会将 booking.approved 的 false 值更改为 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);
}
};

这是我的预订 Controller :

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;

请帮忙。

最佳答案

很难说错误的根本原因是什么,但是从 Promise 返回后 booking 变量被设置为 null。数据库中未找到 Booking,或者未通过请求参数传入 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);

}
}

关于javascript - 如何修复 "TypeError: Cannot set property ' 已批准“为空”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58295393/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com