首页 > 解决方案 > 无法将数据从 express 传递到 ejs?

问题描述

res.send("filename.ejs",data我尝试将数据从 js 文件(sample.js)传递给ejs

TypeError:无法在字符串上创建属性“_locals”

有人可以帮助解决这个问题并告诉我如何在 ejs 文件中调用它们

标签: mysqlexpressejs

解决方案


res.send() 用于发送数据不需要指向 ejs 文件

res.send 将数组作为参数 (res.send([body])) ,您可以在 ejs 中获取它,例如 {{ data }}

例如

NODEJS res.send({message: 'hello'})

文件名.ejs

<div>{{ message }}</div> or <%= JSON.stringify(message)  %>

也因为 Express 5x 不支持res.send()您可以使用的方法

res.status(200).send({message: 'hello})

(你不承认你是快递版)


推荐阅读