Node js 如何使用 json 解析数据数组从 api 调用到 html 索引页面中的渲染/sho

编程入门 行业动态 更新时间:2024-10-03 14:21:03

Node js 如何使用 json 解析数据<a href=https://www.elefans.com/category/jswz/34/1771288.html style=数组从 api 调用到 html 索引页面中的渲染/sho"/>

Node js 如何使用 json 解析数据数组从 api 调用到 html 索引页面中的渲染/sho

我所有的代码都运行正常,但我不知道如何使用这个 dataj (DATAJ) 数组在视图 index.html 的 html 元素中显示,我需要一些指导,经过多次尝试我做不到....


index: 函数 (req, res) {

    const https = require('https');

    CityModel.find().sort({name: 1}).exec(function (error, cities) {
        if (error) {
            return res.render('web/index', { url,dataj: [], cities: [], moment })
        }

        PostModel.find({ status: 'Publicado', main: true }).sort({name: 1}).exec(function (err, posts) {
            if (err) {
                return res.render('web/index', { url, cities, posts: [], moment });

            }

从这里我需要使用响应信息

            var options = {
                host: 'host',
                path: 'ruta',
                headers: {'api-key': 'apitext'}
            };
            
            https.get(options, function (res) {
                var json = '';
                res.on('data', function (chunk) {
                    json += chunk;
                });
                res.on('end', function () {
                    if (res.statusCode === 200) {
                        try {
                            var dataj = JSON.parse(json)
                            .map(ciudad => { 
                                console.log( {ciudades:ciudad.name}) 
                                console.log(ciudad.name)

console.log(ciudad.tag)

                              }); 
                            
                              console.log(json)
                              console.dir(dataj.body);
                            
                            ;
    
    
                        } catch (e) {
                            console.log('Error parsing JSON!');
                        }
                    } else {
                        console.log('Status:', res.statusCode);
                    }
                });
            }).on('error', function (err) {
                  console.log('Error:', err);
            });
    


            return res.render('web/index', { url, cities, posts, moment });


            
        })



    })


},
回答如下:

更多推荐

Node js 如何使用 json 解析数据数组从 api 调用到 html 索引页面中的渲染/sho

本文发布于:2024-05-30 20:51:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   如何使用   索引   页面   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!