Javascript mongoosejs-find()使用嵌套对象

Javascript mongoosejs-find()使用嵌套对象,javascript,mongodb,express,mongoose,Javascript,Mongodb,Express,Mongoose,这个问题可能是重复的,但我还没有找到任何能为我的问题提供适当答案的东西 我有一个ExpressJS服务器,用于提供从MongoDB数据库检索数据的API请求。我在MongoDB连接中使用mongoosejs来查询/保存数据 我正在构建一个路由,该路由允许我查找与某些用户输入匹配的所有数据,但在执行查询时遇到了问题。我花了很长时间在网上寻找一个有类似问题的人,但结果却是一片空白 我将在下面的一分钟留下我的代码示例 路线代码 // -- return matched data (GET) route

这个问题可能是重复的,但我还没有找到任何能为我的问题提供适当答案的东西

我有一个ExpressJS服务器,用于提供从MongoDB数据库检索数据的API请求。我在MongoDB连接中使用mongoosejs来查询/保存数据

我正在构建一个路由,该路由允许我查找与某些用户输入匹配的所有数据,但在执行查询时遇到了问题。我花了很长时间在网上寻找一个有类似问题的人,但结果却是一片空白

我将在下面的一分钟留下我的代码示例

路线代码

// -- return matched data (GET)
router.get('/match', async (req, res) => {

    const style_data = req.query.style; // grab url param for style scores ** this comes in as a string **
    const character_data = req.query.character; // grab url param for character scores ** this comes in as a string **

    // run matcher systems
    const style_matches = style_match(style_data);

    res.send({
        response: 200,
        data: style_matches
    }); // return data

});
查询代码

// ---(Build the finder)
const fetch_matches_using = async function(body, richness, smoke, sweetness) {
    return await WhiskyModel.find({
        'attributes.body': body,
        'attributes.richness': richness,
        'attributes.smoke': smoke,
        'attributes.sweetness': sweetness
    });
}

// ---(Start match function)---

const style_match = async function (scores_as_string) {

    // ---(extract data)---
    const body = scores_as_string[0];
    const richness = scores_as_string[1];
    const smoke = scores_as_string[2];
    const sweetness = scores_as_string[3];

    const matched = [];

    // ---(initialise variables)---
    let match_count = matched.length;

    let first_run; // -> exact matches
    let second_run; // -> +- 1
    let third_run; // -> +- 2
    let fourth_run; // -> +- 3

    // ---(begin db find loop)---

    first_run = fetch_matches_using(body, richness, smoke, sweetness).then((result) => {return result});

    matched.push(first_run);

    // ---(return final data)---
    
    return matched

}
数据库对象的示例

{
 _id: mongoid,
 meta-data: {
  pagemd:{some data},
  name: whiskyname
  age: whiskyage,
  price: price
 },
 attributes: {
  body: "3",
  richness: "3",
  smoke: "0",
  sweetness: "3",
  some other data ...
 }
}
当我在postman中点击路线时,JSON数据如下所示:

{
response: 200,
data: {}
}
当我按下按钮后,console.log()从style match函数中不匹配时,它会打印我不理解的[Promise(pending)]

如果我在console.log()中输入.then()中的结果,则会得到一个空数组

在运行find之后,我尝试了使用populate()方法,该方法在技术上是有效的,但不是只返回与它匹配的数据,而是返回集合中的每个条目,因此我认为我在那里做了一些错误的事情,但我也不明白为什么我需要使用.populate()函数来访问嵌套对象

我是不是做错了什么

我还应该提到,路由和匹配函数位于不同的文件中,只是为了尽量使事情简单


谢谢你的回答。

我刚刚发布了一个答案,因为我似乎已经解决了这个问题

问题在于my.find()函数,它需要传入要搜索的项目,然后还要在函数中回调以返回错误/数据。我将在下面留下更改后的代码

新功能

const fetch_matches_using = async function(body, richness, smoke, sweetness) {
    const data = await WhiskyModel.find({
        'attributes.body': body,
        'attributes.richness': richness,
        'attributes.smoke': smoke,
        'attributes.sweetness': sweetness
    }, (error, data) => { // new ¬
        if (error) { 
            return error;
        }

        if (data) {
            console.log(data)
            return data
        }
    });

    return data; //new
}
将找到的结果发送回路线仍然存在问题,但我认为这是另一个问题。如果它是连接的,我将编辑这个答案,并修复它