javascript - Getting undefined for a key that exists -
i new node.js don't think should happening:
const snekfetch = require('snekfetch'); const auth = require('../auth.json'); const totext = require('html-to-text'); exports.run = async (bot, message, args) => { const content = args.join(" "); const tagged = args[0]; snekfetch.get("http://api.stackexchange.com/2.2/search?pagesize=1&order=desc&sort=votes&tagged="+tagged+"&intitle="+content+"&site=stackoverflow&key=censored") .then(r => console.log(r["body"]["items"]["accepted_answer_id"])); /* var text = htmltotext.fromstring(bodyofsnek, { wordwrap: 130 }); */ }
this prints out undefined. removing ["accepted_answer_id"] gives me object:
[{"tags":["javascript","object","undefined"],"owner":{"reputation":60056,"user_id":797,"user_type":"registered","accept_rate":68,"profile_image":"https://www.gravatar.com/avatar/aef85130bf44caa9b4de0a3153e758f2?s=128&d=identicon&r=pg","display_name":"matt sheppard","link":"https://stackoverflow.com/users/797/matt-sheppard"},"is_answered":true,"view_count":857526,"protected_date":1335343521,"accepted_answer_id":416327,"answer_count":37,"score":2180,"last_activity_date":1504076618,"creation_date":1219735508,"last_edit_date":1447031189,"question_id":27509,"link":"https://stackoverflow.com/questions/27509/detecting-an-undefined-object-property","title":"detecting undefined object property"}]
and can see accepted_answer_id in there value 416327. why returning undefined?
items array. need reference index value, not key name.
for example:
r["body"]["items"][0]
Comments
Post a Comment