如何使用node.js和mongoose处理从mongodb查询中找到的数据



我正试图处理从查询中找到的数据。但是,由于某种原因,我无法从中得到结果。

我能看到它从查询中找到2条记录,但我不能得到实际的记录。不知道我在哪里搞砸了

const channelQuery =  Channel.find({ fields: { $all: channelFieldsFieldNames } })

let fetchedChannels;
channelQuery
.then(documents => {
fetchedChannels = documents;
return Channel.count();
})
.then(count => {
console.log({
message: "Channels fetched successfully!",
channels: fetchedChannels,
maxChannels: count
});
})
.catch(error => {
console.log(error)
res.status(500).json({
message: "Fetching channels failed!"
});
});

这是我在终端中看到的:

{
message: 'Channels fetched successfully!',
channels: [],
maxChannels: 2
}

很奇怪,因为我在应用程序的其他地方使用了相同的代码片段它完美地返回了计数和记录,但这次不是

所以我只想知道如何处理响应

中的记录我发现了一些类似的stackoverflow帖子,但他们没有讨论他们如何处理数据,他们只谈论查询本身如何使用"like"查询MongoDB

您正在使用两个不同的查询:
return Channel.count();=>这里你计算mongo
中所有的channel,而你的搜索const channelQuery = Channel.find({ fields: { $all: channelFieldsFieldNames } })没有找到

最新更新