javascript - 承诺即使在解决后仍处于未决状态
问题描述
我想获取房间数组并将其分配给每个属性 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> }
}
提前致谢。
解决方案
那是因为您在方法之外记录了承诺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
像同步值一样使用它
推荐阅读
- javascript - 如何从Javascript中的数组中提取多个元素?
- c# - 值元组初始化是违反直觉的
- c++17 - 可以检测 std::vector 的 C++ 模板类函数
- python - 如何使用 struct.pack 转换可变长度的整数元组?
- google-sheets - 引用包含特定日期的单元格
- android - React Native 0.61.5 应用程序从 0.61.4 升级后一直停止
- google-apps-script - 谷歌照片API添加照片不起作用,上传似乎工作
- javascript - 我的页面出现问题,因为 onclick 音频不工作
- javascript - 使用 Lodash 从嵌套数组中删除对象
- visual-studio - 升级后Unity编译错误