gpt4 book ai didi

javascript - json.stringify 返回 [object 对象]

转载 作者:行者123 更新时间:2023-12-02 16:45:37 25 4
gpt4 key购买 nike

我有node-express-ejs和cassandra数据库。我正在从 cassandra 的 index.js 文件中读取一个表。

router.get('/index', function(req, res) {
client.connect(function(err) {});
client.execute('SELECT * FROM data.test;', function(err, result) {
var user = result.rows[2];
console.log("here is the user", result);
res.render('index', {
"index": result
});
});
});

我的结果日志如下:

我正在尝试将此结果传递到 .ejs 文件并从中获取时间列。

//我知道用jade。我们这样写

//对于索引中的每个a

//a.time -> 为您提供每次输入

我正在尝试使用 ejs 获得相同的结果。在index.js文件中

 var myVar = <%- JSON.stringify(index) %>; 
document.getElementById("pil").innerHTML= myVar;

但这只是输出

用户界面上的[对象对象]

谁能解释一下如何在 ejs 中做到这一点吗?我想获取数组或其他内容的时间条目,但我不明白如何继续进行。

enter image description here

最佳答案

1

如果你想将变量从jade文件传递到index.js文件,你应该这样做

index.jade

script.
var myVar = !{JSON.stringify(index)}
script(src='/javascripts/index.js')

div(id='pil')

在我们的例子中,myVar 将是对象,因此 index.js 应该如下所示

index.js

window.onload = function () {
document.getElementById('pil').innerHTML = JSON.stringify(myVar);

// show time for first item in array
// document.getElementById('pil').innerHTML = myVar.rows[0].time;
};

我们调用 stringify 是因为正如我之前所说,myVar 是对象,因此我们需要将其转换为字符串。

2

如果你只想获取jade文件中的值,你可以这样做

index.jade

div(id='pil')
=index.rows[0].time

index.rows 是包含对象的数组,因此您需要指定数组的索引(在我们的例子中我们指定了 index.rows[0])并获取您想要的字段,例如 .time。

关于javascript - json.stringify 返回 [object 对象],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27142879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com