首页 > 解决方案 > 承诺即使在解决后仍处于未决状态

问题描述

我想获取房间数组并将其分配给每个属性 wrt 他们的 property_id,但返回的值是一个未决的承诺。不知道出了什么问题。虽然当我记录rooms内部时then它确实正确记录了值。console.log(property) 的结果如下所示。

const vendorProfile = catchAsync(async (req, res, next) => {
  await passport.authenticate("vendor-jwt", { session: false }, (err, user, info) => {
    if (err) {
      res.error = err || info.message;
      return next(401);
    }
    if (!user) {
      res.error = info.message;
      return next(401);
    }
    return Promise.resolve(
      getVendorProfileInfo(user._id)
        .then((result) => {
          if (result == "error") {
            res.error = "Failed to fetch Vendor Profile";
            next(500);
          }
          return getPropertyByVendorId(result._id).then((prop) => {
            for (const property of prop) {
              property._doc.property_rooms = getAllRooms(property._id).then((rooms) => rooms);
              console.log(property);
            }
            res.message = "Vendor Profile fetched successfully";
            res.data = {
              vendor_info: result,
              vendor_properties: prop,
            };
            return next(200);
          });
        })
        .catch((err) => {
          Logger.error(err);
          res.error = "Failed to get vendor profile";
          return next(500);
        })
    ).catch((err) => {
      Logger.error(err);
      res.error = "Failed to get vendor profile";
      return next(500);
    });
  })(req, res, next);
});

这是获取该 property_id 的所有房间的函数:

const getAllRooms = (propertyId) => {
  return Promise.resolve(Room.find({ property_id: propertyId }).then((result) => result)).catch((err) => {
    Logger.error(err);
    return "error";
  });
};

这是我的console.log(property)

{
  property_basic_info: {
    property_name: 'Welcome',
    property_star_rating: 1,
    property_booking_since: 2021,
    property_channel_manager: ''
  },
  property_location: {
    property_geo_loc: { coordinates: [Array], type: 'Point' },
    property_locality: 'bhandup',
    property_address: 'MAHAVIR UNIVERSE',
    property_country: 'India',
    property_state: 'Maharashtra',
    property_city: 'Mumbai',
    property_zip_code: '400078'
  },
  property_contact_details: { phone_no: '7059462868', email: 'roy.srijan@outlook.com' },
  property_amenities: {
    basic_facilities: [ 'Electricity', 'Air Conditioning', 'Elevator/ Lift', 'Bathroom' ],
    general_services: [ 'Food', 'Bellboy service' ],
    outdoor_activities_sports: [],
    common_area: [],
    food_drink: [],
    health_wellness: [],
    business_center_conference: [],
    beauty_spa: [],
    security: []
  },
  property_policies: {
    checkin_time: '10:06',
    checkout_time: '22:06',
    cancellation_policy: 'Free cancellation upto 48 hrs'
  },
  property_rules: {
    id_proof: {
      acceptable_identity_proofs: 'Adhaar',
      unacceptable_identity_proofs: 'Adhaar',
      allow_same_id: true
    },
    guest_profile: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ],
    general_safety_hygiene_guidelines: [],
    room_safety_hygiene: [],
    social_distancing: [],
    food_drinks_hygiene: [],
    property_restrictions: [],
    pet_policy: [],
    guest_suitabilty: [],
    checkin_checkout_policy: [],
    extra_bed_policy: [ [Object] ],
    custom_policy: []
  },
  property_finance_legal: { gst_details: '29AAACR4849R2ZG' },
  property_status: 1,
  property_photo_id: [],
  _id: 61607791b1af193c7b8b9f08,
  vendor_id: 61607775b1af193c7b8b9f07,
  createdAt: 2021-10-08T16:53:37.734Z,
  updatedAt: 2021-10-08T16:53:37.734Z,
  __v: 0,
  property_rooms: Promise { <pending> }
}

提前致谢。

标签: javascriptpromise

解决方案


那是因为您在方法之外记录了承诺then

承诺是异步解决的,因此外部then尚未解决。

你必须改变这一行:

property._doc.property_rooms = getAllRooms(property._id).then((rooms) => rooms);
console.log(property);

property._doc.property_rooms = getAllRooms(property._id).then((rooms) => console.log(rooms));

async/await像同步值一样使用它


推荐阅读