EJS中未定义的变量

时间:2019-07-06 11:19:58

标签: javascript html node.js express ejs

我想知道如何在EJS中访问/获取对象内的对象。 当我尝试使用服务器在EJS中呈现的对象时,收到以下错误:

ser.js

var express = require('express');
var router = express.Router();
router.get('/', function (req, res) { 
    var obj = {"cn": "TH", "contact": {"name": "contactus"}, "about": {"name": "aboutus"}}
    res.render('index.ejs', {result: obj});
})

index.ejs

<a class="nav-link" href="/en/<%=result['contact']['name']%>">Contact</a> 
// should be /en/contactus

错误:

contact is not defined
    at eval (eval at compile (C:\Projects\pro\node_modules\ejs\lib\ejs.js:592:12), <anonymous>:47:25)
    at eval (eval at compile (C:\Projects\pro\node_modules\ejs\lib\ejs.js:592:12), <anonymous>:62:10)
    at returnedFn (C:\Projects\pro\node_modules\ejs\lib\ejs.js:623:17)
    at tryHandleCache (C:\Projects\pro\node_modules\ejs\lib\ejs.js:251:36)
    at View.exports.renderFile [as engine] (C:\Projects\pro\node_modules\ejs\lib\ejs.js:480:10)
    at View.render (C:\Projects\pro\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Projects\pro\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Projects\pro\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Projects\pro\node_modules\express\lib\response.js:1008:7)
    at util.getJsonData.then.data (C:\Projects\pro\routes\language.js:18:27)

0 个答案:

没有答案