简体   繁体   English

如何将Pug中的JSON数据发送到JavaScript

[英]How send JSON data from pug to javascript

I send a JSON object from my mangoDB to the html page in this way: 我以这种方式将MangoDB中的JSON对象发送到html页面:

router.get('/index', function (req, res, next) {
    GenoverseInstance.find({name: req.query.name}, function (err, instance) {
        if (err) {
            res.send(err);
            throw err;
        } else if (instance.length) {
            console.log('Object loaded');
            // object of the user
            console.log(instance[0]);
            res.render('index', {object: instance[0]});
        }
    });

});

I can use it in the html like this: 我可以像这样在html中使用它:

.containerCustom
  .head
    h1
      | #{object.name}

But I can not use it in my javascript which is included in the html page: script. 但是我无法在html页面:脚本中包含的javascript中使用它。

alert(object.name);

How is it possible? 这怎么可能?

Thanks 谢谢

object is only defined in your Pug template and used to generate HTML that is then sent to the browser. object仅在您的Pug模板中定义,并用于生成HTML,然后将其发送到浏览器。 After the HTML is generated, this object is consumed and disappears. 生成HTML之后,该object被使用并消失。 It has nothing to do with the page's JS code. 它与页面的JS代码无关。

If you want this data to be available in the JS code, then : from the generated page, make another (Ajax) request to your server, asking for this same data. 如果您希望这些数据在JS代码中可用,则:在生成的页面上,向服务器发出另一个(Ajax)请求,并请求相同的数据。

this is because your response is saved in local scope and you don't pass that response to global scope where u can access it from outside. 这是因为您的响应保存在本地范围中,并且您不会将该响应传递给全局范围,在此您可以从外部访问它。 I just make 1 snippet for you. 我只为您制作1个摘要。 Check it, i hope this will help you. 检查一下,希望对您有所帮助。 Also if you don't understand scopes i suggest you to go and read some articles, like w3school or this . 另外,如果您不了解示波器,建议您去阅读一些文章,例如w3schoolthis Also if you don't know what is asynchronous request read about them too. 另外,如果您不知道什么是异步请求,请阅读有关它们的信息。

 /* ############################### */ // So if you want to access response from your request you must save them or pass them // This is one way to have access to your response outside by saving to global variable // Creating variable where we will hold our response from request // Global scope start here var data = ''; function getResponse(res) { // Here is opened Local scope where u have no access from outside // and now here u have the response from your request console.log('function data', res); // you can do stuff here with that response data = res; } setTimeout(function() { var response = [1,2,3]; // Now here we will save response to our data so we can access it from global scope // But remember that is async request data = response; console.log("local scope response ", response); }, 1000); setTimeout(function() { console.log("global scope data", data); // here data hold ur response data from your async request }, 1100); /* ############################### */ // This is another way to have access to your response outside by passing it to function function getResponse(res) { // and now here u have the response from your request console.log('function data', res); // you can do stuff here with that response data = res; } setTimeout(function() { var response = [1,2,3]; // other way pass response to your function and do what u want with that data getResponse(response); console.log("bracked scope response ", response); }, 1000); 

As shown in the comments this link is very useful reference , thanks to @IsaacGodinez. 如评论中所示,由于@IsaacGodinez,此链接是非常有用的参考 It's possible to use this line of code to get the entire object: 可以使用以下代码行来获取整个对象:

var data = !{JSON.stringify(object).replace(/<\//g, '<\\/')};

Or if you just want an element of the object: 或者,如果您只想要对象的元素:

var name = "#{object}";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM