Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将MongoDB集合中的数组中的特定对象获取到handlebars表中_Javascript_Mongodb_Express_Mongodb Query_Handlebars.js - Fatal编程技术网

Javascript 将MongoDB集合中的数组中的特定对象获取到handlebars表中

Javascript 将MongoDB集合中的数组中的特定对象获取到handlebars表中,javascript,mongodb,express,mongodb-query,handlebars.js,Javascript,Mongodb,Express,Mongodb Query,Handlebars.js,我试图用mongodb中的对象数组中的值构建一个表,但我只能获取每个值,并且只需要特定的值 所以我提出了这样一个问题 router.get("/arquiveExpense", ensureAuthenticated, (req, res) => { House.find({ userID: req.user.id, expensesHouse: { $elemMatch: { status: "Private" } } }).then(house => {

我试图用mongodb中的对象数组中的值构建一个表,但我只能获取每个值,并且只需要特定的值

所以我提出了这样一个问题

router.get("/arquiveExpense", ensureAuthenticated, (req, res) => {
  House.find({
    userID: req.user.id,
    expensesHouse: { $elemMatch: { status: "Private" } }
  }).then(house => {
    console.log(house);
    res.render("houses/arquiveExpense", {
      house: house
    });
  });
});
我想从状态为“Private”的expensesHouse检索特定值

在车把上我有这种结构

<tbody>

                    <tr>
                        {{#each house}}

                        <td>{{expenseType}}</td>
                        <td>{{price}}€</td>
                        <td>{{payAt}}</td>

                        <td>{{formatDate date 'MMMM Do YYYY'}}</td>
                        <td>
                            <a href="/houses/showExpense/{{id}}" id="detailsExpense"
                                class="btn btn-outline-light mb-3"><i class="fas fa-eye mr-2"></i>Details
                            </a>
                        <td>
                            <a href="/houses/editExpense/{{id}}" id="editExpense" class="btn btn-outline-light mb-3"><i
                                    class="fas fa-edit mr-2"></i>Edit
                        </td>
                    </tr>

                    {{else}}
                    <p>No expenses</p>
                    {{/each}}
                </tbody>

{{{每家每户}
{{expenseType}}
{{价格}}€
{{payAt}}
{{formatDate'MMMM Do YYYY'}

模式结构如下所示

因此,我想在网页中显示状态为“Private”的expensesHouse的值


如何更改代码以仅检索此值?

请使用以下内容更新您的代码,它解决了代码和查询的问题:

router.get("/arquiveExpense", ensureAuthenticated, async (req, res) => {
    try {
        let house = await House.findOne({ // You can use findOne as if userID is unique
            userID: req.user.id
        }, { expensesHouse: { $elemMatch: { status: "Private" } } }) //$elemMatch in projection helps you to get what is needed
        if (house) { // .findOne() returns null if no document found, else an object
            console.log(house);
            res.render("houses/arquiveExpense", {
                house: house
            });
        } else { // No document found
            console.log('No house found')
        }
    } catch (error) {
        console.log('Error at DB call ::', error)
    }
})

这回答了你的问题吗?我可以从mongoDB中检索到正确的信息,但无法在handlebars中构建正确的信息。问题是:在查询mongoDB之后,我得到的信息是正确的,但是当我将它们传递到Handlebar上并查看页面上的内容时,我无法在那里获得值。是不是因为没有等到DB调用完成?试试这个::
router.get(“/arquiveExpense”,确保经过身份验证,异步(req,res)=>{Try{let house=wait house.findOne({userID:req.user.id,expensesHouse:{$elemMatch:{status:'Private}}})如果(house){console.log(house);res render(“houses/arquiveExpense”{house:house});}else{console.log('No house found')}}catch(error){console.log(error)}
它可以工作,但给我的结果与我的相同..问题在于把手,因为当我为每个人做一个#时,我得到所有费用,公共费用和私人费用..你的意思是说你的查询根据
用户id:req.user.id
对用户进行了正确过滤,但是
expensesHouse
数组具有所有状态和私人费用?你是对的吗是的!问题还在等待……非常感谢!@CarlosOrelhas:Yes&还有query,您的初始查询将返回
expensesHouse
中的所有内容,因为这不存在:-)我在“Public”中也犯了同样的错误,所以您通过这种方式帮助我解决了这两个问题!:)