如何从[Object object]
获取数据?
以下是我正在尝试做的一个例子。
// Get data with dirty
var data = db.get('/htmltest')
// My test.db file
{"key":"foo","val":"barwhat?"}
{"key":"/htmltest","val":{"title":"Html Test","content":"<span>This is HTML</span>"}}
// the console.log gives me [Object Object]
// How do I get it to show the content of title (Html Test)
console.log(data);
答案 0 :(得分:2)
根据documentation,get
方法返回给定键的值。在这种情况下,您应该能够访问title
属性,如下所示:
// Get data with dirty
var data = db.get('/htmltest')
// My test.db file
{"key":"foo","val":"barwhat?"}
{"key":"/htmltest","val":{"title":"Html Test","content":"<span>This is HTML</span>"}}
// the console.log gives me [Object Object]
// How do I get it to show the content of title (Html Test)
console.log(data.title);
答案 1 :(得分:2)
您可以采取的另一项措施是使用util
inspect function来帮助您查看调试数据。
var util = require('util');
var data = db.get('/htmltest');
console.log(util.inspect(data));
同样,这仅用于调试和检查对象的内容。
答案 2 :(得分:0)
如果您证明您的data
变量是JSON对象,您也可以轻松解析它并使用JSON.stringify(data);