如何在模型内的数组中选择对象的属性?

时间:2017-03-31 15:50:23

标签: javascript

我的模特是:

var mongoose = require("mongoose");



var invoice = new mongoose.Schema({
    invoiceDate: String,
    invoiceNumber: String,
    orderInfo: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "order"
        }
    ],
    customerInfo: [{
       id: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "customer"
        },
    customerName: String,
    }]
});

//EXPORT
module.exports = mongoose.model("invoice", invoice);

我希望以这种方式显示数据:

<tbody>
    <%data.forEach(function(data){%>
        <tr>
            <td><a href="/showInvoice/<%=data._id%></td>
            <td>
                <%=data._id%>
            </td>
            <td>
                <%=data.invoiceDate%>
            </td>
            <td>
                <%=data.invoiceNumber%>
            </td>
            <td>
                <%=data.customerInfo.customerName%>
            </td>
        </tr>
        <%});%>
</tbody>

但我收到的错误是:

  

不能使用未定义的属性customerName

这很奇怪因为<%=data.customerInfo%>有效并向我展示了一个对象,但我只需要customerName属性。

0 个答案:

没有答案