TypeError:无法读取属性' then'使用诺言的不确定
我正在将此网站作为一个项目,这是Airbnb的“沃尔玛”版本。
以下是该按钮应为上下文所做的:
用户将单击列表上的“预订”按钮。 他们将选择一个开始日期和结束日期,然后提交。这将向服务器发送HTTP请求。
但是,我正在遇到返回的错误:
typeError:无法读取未定义的属性“然后” at/vagrant/lightbnb/lightb_web_app/server/server/apiroutes.js:44:7< -
第44:7行是下面的API路由:
这是错误的:
.then((reservation) => {
res.send(reservation);
这是API Route Logic,它引起了一个问题:引起问题:
router.post('/reservations', (req, res) => {
const userId = req.session.userId;
database
.addReservation({ ...req.body, guest_id: userId })
.then((reservation) => {
res.send(reservation);
})
.catch((e) => {
console.error(e);
res.send(e);
});
});
该路线正在调用函数addReserveration(),这是以下内容:
/**
* Add a reservation to the database
* @param {{}} reservation An object containing all of the reservation details.
* @return {Promise<{}>} A promise to the reservation.
*/
const addReservation = function (reservation) {
const queryString = `
INSERT INTO reservations(
start_date,
end_date,
property_id,
guest_id
)
VALUES ($1, $2, $3, $4)
RETURNING *
`;
const values = [
reservation.start_date,
reservation.end_date,
reservation.property_id,
reservation.guest_id,
];
pool
.query(queryString, values)
.then((res) => {
res.rows;
})
.catch((e) => console.log(e.message));
};
exports.addReservation = addReservation;
如果您需要更多信息,请告诉我。
I am making this website as a project which is a "Walmart" version of AirBnB.
Here's what the button is supposed to do for context:
A user will click on a "Make Reservation" button on a listing.
They will select a start and end date then submit. This will send an HTTP request to the server.
However, I am running into an error where it returns :
TypeError: Cannot read property 'then' of undefined
at /vagrant/LightBnB/LightBnB_WEB_APP/server/apiRoutes.js:44:7 <--
The line 44:7 is the API route below:
This is causing the error:
.then((reservation) => {
res.send(reservation);
This is the API ROUTE Logic which is causing an issue:
router.post('/reservations', (req, res) => {
const userId = req.session.userId;
database
.addReservation({ ...req.body, guest_id: userId })
.then((reservation) => {
res.send(reservation);
})
.catch((e) => {
console.error(e);
res.send(e);
});
});
That route is calling the function addReservation() which is the following:
/**
* Add a reservation to the database
* @param {{}} reservation An object containing all of the reservation details.
* @return {Promise<{}>} A promise to the reservation.
*/
const addReservation = function (reservation) {
const queryString = `
INSERT INTO reservations(
start_date,
end_date,
property_id,
guest_id
)
VALUES ($1, $2, $3, $4)
RETURNING *
`;
const values = [
reservation.start_date,
reservation.end_date,
reservation.property_id,
reservation.guest_id,
];
pool
.query(queryString, values)
.then((res) => {
res.rows;
})
.catch((e) => console.log(e.message));
};
exports.addReservation = addReservation;
Please let me know if you need more information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有返回值,因此它返回
undefined
。因此,当您尝试执行addReservation(...)。然后(...)
时,您最终尝试访问。
导致您遇到的错误。在
AddReserveration()
的内部,您需要更改:为此
,您的承诺可以返回您的承诺,然后呼叫者可以在返回的承诺中使用
。
请注意,您的
.catch()
AddReservation()
正在记录,然后“吃”错误。您可能应该将错误重新验证,以便呼叫者可以看到错误:更改以下内容:
对此:
另请注意,
res.send(e)
可能不会提供太多有用的信息,因为大多数属性的大多数属性错误对象无法枚举,因此当res.send()
将错误对象变成JSON时不会显示。addReservation()
has no return value, thus it returnsundefined
. So, when you try to doaddReservation(...).then(...)
, you end up trying to access.then()
onundefined
which results in the error you get.Inside of
addReservation()
, you need to change:to
That returns your promise so then the caller can use
.then()
on the returned promise.Note, your
.catch()
handler inaddReservation()
is logging and then "eating" the error. You should probably rethrow the error so that the caller can see the error:Change this:
to this:
Also, note that
res.send(e)
probably won't provide much useful info because most properties of the Error object are not enumerable and thus won't show up whenres.send()
turns the Error object into JSON.