gpt4 book ai didi

javascript - 使用异步等待时无法获取对象的属性

转载 作者:行者123 更新时间:2023-12-01 01:51:43 25 4
gpt4 key购买 nike

我正在使用 Node.js 和 mongoose,但这个问题纯粹是关于 async await 部分。我编写了这段代码,并且它有效。

router.get('/applications/:_id', async (req, res, next) => {
var posting = await Posting.findOne({'_id': req.params._id, 'creatorId': req.user._id, 'visible': true}); // await here
var seekers = await Account.find({'accType': 'seeker', 'blocked': false});
console.log(posting.applications) // no await here
// ^^^ this will log the applications array.
res.render('users/employer/applications', {
user: req.user,
title: 'Job applications',
applications: posting.applications,
seekers: await seekers,
})
});

但是如果我这样做:

router.get('/applications/:_id', async (req, res, next) => {
var posting = Posting.findOne({'_id': req.params._id, 'creatorId': req.user._id, 'visible': true}); // remove await from here
var seekers = Account.find({'accType': 'seeker', 'blocked': false});

console.log(await posting.applications); // add await here
// ^^^ this will log undefined
res.render('users/employer/applications', {
user: req.user,
title: 'Job applications',
applications: await posting.applications,
seekers: await seekers,
})
});

我无法打印 posting.applications 数组。 (未定义)。

但是我可以像这样打印出整个对象:

router.get('/applications/:_id', async (req, res, next) => {
var posting = Posting.findOne({'_id': req.params._id, 'creatorId': req.user._id, 'visible': true});
// ^^^ remove await from here
var seekers = Account.find({'accType': 'seeker', 'blocked': false});

console.log(await posting); // add await here
// ^^^ this will log the object
console.log(await posting.applications); // this will log undefined
res.render('users/employer/applications', {
user: req.user,
title: 'Job applications',
applications: await posting.applications,
seekers: await seekers,
})
});

我认为await会停止代码的执行,直到它完成为止。那么,如果对象存在,为什么我无法获取属性呢?

[重复注释]我还通过将对象发送到 View 来检查这一点。它在 View 中的行为方式与在 console.log 中的行为方式相同。这是一个简化的示例。

最佳答案

在第一个示例中:

var posting = await Posting.findOne({'_id': req.params._id, 'creatorId': req.user._id, 'visible': true}); // await here
var seekers = await Account.find({'accType': 'seeker', 'blocked': false});

您正在等待 Posting.findOneAccount.find 返回的 promise ,并将这些 promise 的分辨率值存储在 发帖搜索者

在第二个示例中:

var posting = Posting.findOne({'_id': req.params._id, 'creatorId': req.user._id, 'visible': true}); // remove await from here
var seekers = Account.find({'accType': 'seeker', 'blocked': false});

console.log(await posting.applications); // add await here

...您将 promise 对象本身存储在postingseekers中,然后获取属性applications 来自 posting 中的 promise 。 Promise 没有名为 applications 的属性,因此 posting.applications 会导致 undefinedawait非 promise 值会返回该值本身,因此 await posts.applications 会为您提供未定义

在你的第三个例子中:

var posting = Posting.findOne({'_id': req.params._id, 'creatorId': req.user._id, 'visible': true});
// ^^^ remove await from here
var seekers = Account.find({'accType': 'seeker', 'blocked': false});

console.log(await posting); // add await here

...您再次将 promise 存储在发布中,但随后在执行日志时等待该 promise ,因此分辨率值> 进入 console.log。但 posting 仍然指的是 promise ,而不是其结果,因此 posting.applications 仍然是未定义

在第三个示例中,您可以这样做:

posting = await posting;

...但实际上,你的第一个例子很好。

如果您希望 Posting.findOneAccount.find 重叠,则可以使用 Promise.all:

var [posting, seekers] = await Promise.all([
Posting.findOne({'_id': req.params._id, 'creatorId': req.user._id, 'visible': true}),
Account.find({'accType': 'seeker', 'blocked': false})
]);

关于javascript - 使用异步等待时无法获取对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51442451/

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