Node.js 脚本标记可以获取EJS变量吗?

Node.js 脚本标记可以获取EJS变量吗?,node.js,maps,ejs,Node.js,Maps,Ejs,我正在构建一个带有NodeJS后端和EJS的应用程序。我想创建一个带有一些标记的地图。这些标记是数据库中的一些城市和国家的名称。我的问题是:我能否以某种方式将EJS用于脚本标记?我做了些什么,但我的IDE给我带来了一个错误。如果可能的话,你能告诉我怎么做吗 以下是我的职能: 节点JS exports.getMap = (req,res,next) => { const notename = req.params.notename; Note.findOne({notenam

我正在构建一个带有NodeJS后端和EJS的应用程序。我想创建一个带有一些标记的地图。这些标记是数据库中的一些城市和国家的名称。我的问题是:我能否以某种方式将EJS用于脚本标记?我做了些什么,但我的IDE给我带来了一个错误。如果可能的话,你能告诉我怎么做吗

以下是我的职能:

节点JS

exports.getMap =  (req,res,next) => {

    const notename = req.params.notename;
  Note.findOne({notename:notename}).then(note => {

    res.render('feed/map', {
        pageTitle:'Pedigree Notes | MAP',
        path:'/getmap',
        locations:note.locations

      });
  })


}
还有客户端

<%- include('../includes/head.ejs'); %>
<%- include('../includes/navigation.ejs');%>

<main>
<div class="container-fluid">

    <div id="map">

    </div>
</div>

<script>
var map;
var geocoder;
function initMap(){
   var mapLayer = document.getElementById('map')
   var centerCoordinates = new google.maps.LatLng(40.4637, 3.7492);
   var defaultOptions = { center:centerCoordinates, zoom:2};

   map = new google.maps.Map(mapLayer,defaultOptions);
   geocoder = new google.maps.Geocoder();
   <% for(let location of locations) { %>
   <% } %>

}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC8QqwujgKgEaAk2_MvUph9Jows_P1eILs&callback=initMap"
async defer></script>
</main>

<%- include('../includes/end.ejs'); %>

var映射;
var地理编码器;
函数initMap(){
var mapLayer=document.getElementById('map'))
var centerCoordinates=new google.maps.LatLng(40.4637,3.7492);
var defaultOptions={中心:中心坐标,缩放:2};
map=新的google.maps.map(mapLayer,defaultOptions);
geocoder=新的google.maps.geocoder();
}
我如何编写它以达到从后端解析的值


谢谢大家!

您不能使用模板循环-它只会在服务器上循环,并生成成倍的JS代码,这不是您想要的。使用此模式:

const locations = <%- JSON.stringify(locations) %>;

(如果您坚持:p,则为循环使用等效的

我的IDE仍然“红色”该行,但这只是视觉效果,它正在工作,非常感谢!6分钟后我会接受答案!如果您的IDE正在检查JS语法,这是正常的-这是EJS。
locations.forEach(location => {
    ...
});