显示从节点MySQL结果到页面的把手的数据

时间:2019-03-04 19:07:48

标签: javascript node.js express handlebars.js axios

我有一条路由,它从结果对象中的数据库返回一行结果。

app.get('/newusersvariables', function(req, res) {

const sessionid = req.session.id;

//let newuser = mysql.createConnection(mysqlconfig);
let connection = mysql.createConnection(mysqlconfig);

connection.connect(function(err) {
    if (err) throw err;
    connection.query("SELECT * FROM user_idpdetails WHERE sessionid = ?", [sessionid], function (err, result, fields) {     
        if (err) throw err;

    var userdetails = result;       
    res.send(userdetails);

    });
connection.end();   
}); 
});

现在,我正在尝试在页面上显示此数据...

            <script id="newuser-template" type="text/x-handlebars-template">
                            <p>{{sessionid}}</p>
                            <p>{{userid}}</p>   
                            <p>{{email}}</p>                    
        </script>   

我要使用的车把如下...

            <script type="text/javascript">
            // Get the template
            var source = document.getElementById("newuser-template").innerHTML;

            // Compile the template
            var template = Handlebars.compile(source);

            // Make a request for the whmcs route using axios
            axios.get('/newusersvariables')
                    .then(function (userdetails) {
                        // Get the client data from the response            
            var data = userdetails[0].RowDataPacket;

            //  console.log(data);

                    // Pass it through the template
                    var html = template(JSON.stringify(data));

                    // Set our target divs html with our template html
                    document.getElementById("content").innerHTML = html;
                    })
                    .catch(function (error) {
                        // handle error
                        console.log(error);
                    })  
        </script>    

然后在正文中输出内容div ...

<p></p>
    <div id="content">

数据作为JSON对象传递,但是我不确定我是否从axios函数正确调用了数据...

Data in the dev console

任何帮助将不胜感激。

0 个答案:

没有答案