TypeError:无法读取属性' then'使用诺言的不确定

发布于 2025-02-03 11:17:22 字数 1565 浏览 3 评论 0原文

我正在将此网站作为一个项目,这是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娜些时光,永不杰束 2025-02-10 11:17:22

TypeError:无法读取未定义的

的属性'然后

没有返回值,因此它返回undefined。因此,当您尝试执行addReservation(...)。然后(...)时,您最终尝试访问导致您遇到的错误。

AddReserveration()的内部,您需要更改:

pool.query(...).then(...).catch(...)

为此

return pool.query(...).then(...).catch(...)

,您的承诺可以返回您的承诺,然后呼叫者可以在返回的承诺中使用


请注意,您的.catch() AddReservation()正在记录,然后“吃”错误。您可能应该将错误重新验证,以便呼叫者可以看到错误:

更改以下内容:

.catch((e) => console.log(e.message));

对此:

.catch((e) => {
     console.log(e.message)
     // re-throw the error so it propagates to the caller
     throw e;
});

另请注意,res.send(e)可能不会提供太多有用的信息,因为大多数属性的大多数属性错误对象无法枚举,因此当res.send()将错误对象变成JSON时不会显示。

TypeError: Cannot read property 'then' of undefined

addReservation() has no return value, thus it returns undefined. So, when you try to do addReservation(...).then(...), you end up trying to access .then() on undefined which results in the error you get.

Inside of addReservation(), you need to change:

pool.query(...).then(...).catch(...)

to

return pool.query(...).then(...).catch(...)

That returns your promise so then the caller can use .then() on the returned promise.


Note, your .catch() handler in addReservation() is logging and then "eating" the error. You should probably rethrow the error so that the caller can see the error:

Change this:

.catch((e) => console.log(e.message));

to this:

.catch((e) => {
     console.log(e.message)
     // re-throw the error so it propagates to the caller
     throw e;
});

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 when res.send() turns the Error object into JSON.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文