0

我正在尝试遍历数组以发送客户订购的产品列表。我显示了车把模板,但我的{{#each}}功能不起作用。我正在使用 Nodejs 并通过 POST 请求从我的 React 前端表达和接收数据。

数据返回如下:

{
    order_total:  1200,
    customer_email: roger@mail.com,
    customer_first_name: Roger,
    customer_last_name: Smith,
    customer_address: 1024 Cherry Street,
    customer_city: Langley Falls,
    customer_state: Virginia,
    customer_zip: 11111,
    customer_phone: 123456789,
    status: in progress,
    order_items: [ 
        {
            product_id: 12,
            title: Slay bed,
            image_url: url,
            price: 400,
            quantity: 1,
            color_id: 12,
            image_id: 7
        },
        {
            product_id: 13,
            title: dresser,
            image_url: url
            price: 800,
            quantity: 1,
            color_id: 12,
            image_id: 7
        }
    ]
}   

我将 order_items 存储在一个变量中const order_items = req.body.order_items

我的 msg 对象如下所示:

const msg = {
    to: [order.customer_email],
    bcc: 'test@test.com',
    from: 'test@test.com',
    subject: `Test`,
    html:
        `<head>
            <title></title>
        </head>
        <body>
            <div data-role="module-unsubscribe" class="module" role="module" data-type="unsubscribe" style="color:#444444; font-size:12px; line-height:20px; padding:16px 16px 16px 16px; text-align:Left;" data-muid="4e838cf3-9892-4a6d-94d6-170e474d21e5">
            <p>This is a conformation that your order has been processed.
            You will receive a delivery time within the next 24 hours. Thanks for being a valued customer. Please see order below. If any part of this order is incorrect please reach out to us at test@mail.com</p>
            <h3 style="margin-top:4rem;">Order #${addedOrder}</h3>
            <p style="font-size:12px; line-height:20px;"> Name on order:  ${order.customer_first_name} ${order.customer_last_name}</p>
                <p style="text-transform:capitalize">Address: ${order.customer_address} ${order.customer_city} ${order.customer_state}, ${order.customer_zip}</p>
            <p>Phone Number: ${order.customer_phone}</p>
            <p>Order Status: ${order.status}</p>
                    {{#each order_items}
            
            <div>
                <img style="width:100%;" src="{{this.image_url}}"/>
            </div>
            <p style="font-size:1.3rem" >{{this.title}}</p>
        
            <p style="font-size:1.5rem">{{this.price}}</p>
            
            <p>Total: $800</p>    
            {{/each}}
        </body>`
};
sgMail.send(msg).then(() => {
    console.log('Message sent', msg)
}).catch((error) => {
    console.log(error.response.body)
    console.log(error.response.body.errors[0].message)
})
4

1 回答 1

0

看来您在这一行中忘记了一个括号:

{{#each order_items}

绝对应该是:

{{#each order_items}}

这在您的代码中是否相同?

另外(不太可能但仍然如此),可能有一条规则禁止您在循环中包含许多 html 标签?检查您是否必须将它们包装在一个 div 中(同样我不记得这样的事情......)

于 2020-07-19T08:43:42.990 回答