首页 > 解决方案 > 如何在 node-js 中添加换行符?

问题描述

我正在尝试在 lambda 函数内的节点 js 中的两个字符串之间添加换行符。

我曾尝试使用 '\n' 、 '\r\n' 并尝试导入 os 然后使用 os.EOL 语句,但这些都不起作用。

------------------------Using '\n'-------------------------------

var msg = `The order(s) for the customer ${CustomerNumber} and its status are ${data}`
msg+='\n'
msg+= ' To know the order details of another customer enter the customer 


-------------------------- Using '\r\n' -------------------------------
var msg = `The order(s) for the customer ${CustomerNumber} and its status are ${data}`
msg+='\n'
msg+= ' To know the order details of another customer enter the customer number'

-------------------------- Using 'os.EOL' -------------------------------
var os = require("os");
var msg = `The order(s) for the customer ${CustomerNumber} and its status are ${data}`+os.EOL+' To know the order details of another customer enter the customer number'


-------------- Sending msg string to the AWS lex bot----------------------
callback(elicitSlot(sessionAttributes, deliveryStatus, slots, "CustomerNumber", {

                        'contentType': 'PlainText',
                        'content': msg

                    } ));

function elicitSlot(sessionAttributes, intentName, slots, slotToElicit, message) {
    return {
        sessionAttributes,
        dialogAction: {
            type: 'ElicitSlot',
            intentName,
            slots,
            slotToElicit,
            message,
        },
    };
}

所有这些都有节点效应,我得到的输出没有换行符,并且是一个连续的字符串

我期望的输出是:“客户 abcd 的订单及其状态为 1234、5678、9876”要了解另一个客户的订单详细信息,请输入客户编号

输出我得到的是:“客户 abcd 的订单及其状态为 1234、5678、9876”要了解另一个客户的订单详细信息,请输入客户编号

我认为这是因为我将内容类型指定为“纯文本”,任何人都可以建议我一个解决方案吗?

标签: node.jsaws-lambdaaws-lex

解决方案


看起来这有点旧,但我在教程中找到了答案。要在消息中使用 html,您需要对消息使用反引号。例如:

event.response.emailMessage = `<h1>Welcome to the App!</h1> <p>Your user name is ` + event.request.usernameParameter + `</p> <p>Your temporary password: ` + event.request.codeParameter + `</p> <p>If you need assistance, please contact the help desk at (555) 555-5555 or email <a href="mailto:help@example.org">help@example.org.</a></p>`;

希望这会帮助其他人。


推荐阅读