0

Cuando envío el json a la vista de de mi ejs y intento seleccionar alguna de sus propiedades no me muestra nada.

Mi código:

Express Router

const express = require('express');
const wkd = require('steem');
const router = express.Router();

var query = ['hadrien.rivere'];

let myjson;

wkd.api.getAccounts(query, function(err, res) {
    myjson = res;
});

router.get('/', (req, res) => {
    res.render('../views/index.ejs', {
        enviar: myjson
    });
});

module.exports = router;

El json a usar una vez que se ejecute la función: wkd.api.getAccounts

[
  {
    "id": 51421,
    "name": "hadrien.rivere",
    "owner": {
      "weight_threshold": 1,
      "account_auths": [],
      "key_auths": [
        [
          "WKA5MiBd8XWvnhAtvCJBcvnuPrPco8ybnzQA6RyPhYrZjdy6g3AY1",
          1
        ]
      ]
    },
    "active": {
      "weight_threshold": 1,
      "account_auths": [],
      "key_auths": [
        [
          "WKA5xYEEH9K3ZcUvMgzf4t1QLBkVZDubeoUfrH3Gz7xfJmRdDQpTt",
          1
        ]
      ]
    },
    "posting": {
      "weight_threshold": 1,
      "account_auths": [],
      "key_auths": [
        [
          "WKA5dghrkyYfqQv4RFyPFPNhetktc5j9n58J6qAxNQYyuXacwUaoW",
          1
        ]
      ]
    },
    "memo_key": "WKA5NEg1Y727yXtJFXAXQEHprRkZDRJQfq3b2Cvsi8vtc4g1MbxW5",
    "json_metadata": "{\"profile\":{\"name\":\"Hadrien Rivere\",\"about\":\"Php & Javascript Developer.\",\"location\":\"Ciudad de Mexico\",\"website\":\"https://wekunow.com\",\"profile_image\":\"https://scontent.fmex10-1.fna.fbcdn.net/v/t1.0-9/30709565_1503264209778166_3085169884544368640_n.jpg?_nc_cat=104&_nc_ht=scontent.fmex10-1.fna&oh=6cecbf65434425f822e77680897a5f43&oe=5C6DB52C\",\"cover_image\":\"https://scontent.fmex10-1.fna.fbcdn.net/v/t1.0-9/45358979_1756777437760174_1051862869752152064_n.jpg?_nc_cat=105&_nc_ht=scontent.fmex10-1.fna&oh=3aa95388801763faf786790cd21a91a9&oe=5C9B5290\"}}",
    "proxy": "",
    "last_owner_update": "1970-01-01T00:00:00",
    "last_account_update": "2018-12-25T06:14:51",
    "created": "2018-12-03T18:32:24",
    "mined": false,
    "owner_challenged": false,
    "active_challenged": false,
    "last_owner_proved": "1970-01-01T00:00:00",
    "last_active_proved": "1970-01-01T00:00:00",
    "recovery_account": "initminer",
    "last_account_recovery": "1970-01-01T00:00:00",
    "reset_account": "null",
    "comment_count": 0,
    "lifetime_vote_count": 0,
    "post_count": 104,
    "can_vote": true,
    "voting_power": 9411,
    "last_vote_time": "2019-01-29T17:53:54",
    "balance": "0.000 WEKU",
    "savings_balance": "0.000 WEKU",
    "sbd_balance": "684.704 WKD",
    "sbd_seconds": "469841350419",
    "sbd_seconds_last_update": "2019-01-29T02:39:24",
    "sbd_last_interest_payment": "2019-01-12T22:50:15",
    "savings_sbd_balance": "0.000 WKD",
    "savings_sbd_seconds": "0",
    "savings_sbd_seconds_last_update": "1970-01-01T00:00:00",
    "savings_sbd_last_interest_payment": "1970-01-01T00:00:00",
    "savings_withdraw_requests": 0,
    "reward_sbd_balance": "0.000 WKD",
    "reward_steem_balance": "0.000 WEKU",
    "reward_vesting_balance": "446018.975538 VESTS",
    "reward_vesting_steem": "8.115 WEKU",
    "vesting_shares": "314730428.386420 VESTS",
    "delegated_vesting_shares": "0.000000 VESTS",
    "received_vesting_shares": "11000000.000000 VESTS",
    "vesting_withdraw_rate": "0.000000 VESTS",
    "next_vesting_withdrawal": "1969-12-31T23:59:59",
    "withdrawn": 0,
    "to_withdraw": 0,
    "withdraw_routes": 0,
    "curation_rewards": 76001,
    "posting_rewards": 1264554,
    "proxied_vsf_votes": [
      0,
      0,
      0,
      0
    ],
    "witnesses_voted_for": 0,
    "average_bandwidth": "12091321532",
    "lifetime_bandwidth": "89954000000",
    "last_bandwidth_update": "2019-01-29T17:53:54",
    "average_market_bandwidth": 1250000000,
    "lifetime_market_bandwidth": 1250000000,
    "last_market_bandwidth_update": "2018-12-06T16:18:18",
    "last_post": "2019-01-28T01:32:24",
    "last_root_post": "2019-01-20T03:39:36",
    "vesting_balance": "0.000 WEKU",
    "reputation": "34368576011895",
    "transfer_history": [],
    "market_history": [],
    "post_history": [],
    "vote_history": [],
    "other_history": [],
    "witness_votes": [],
    "tags_usage": [],
    "guest_bloggers": []
  }
]

Mi ejs index.ejs

<% test = JSON.stringify(enviar) %>

<% for (var i in test) { %>
    <%= test[i].name%>
<% } %>

Ayuda :/

4
  • 1
    Hola, me parece que el problema está en que asumes que el resultado devuelto por wkd.api.getAccounts() es un JSON, cuando en realidad es un array que contiene un JSON. ¿Porque no pruebas haciendo lo siguiente: myjson = res[0]; y comentas los resultados? Commented el 30 ene. 2019 a las 15:01
  • efectivamente asi es lo solucione haciendo esto en ejs. <% for (var i in enviar) { %> <% var author = JSON.stringify(enviar[i].author)%> <% var aut = JSON.parse(author)%> <%=aut%> <% } %> El problema es que tengo ahora un json_metadata en el mismo array y no puedo iterar esa sección. :/ {"tags":["community-deals","zealpro","howto","it","technology"],"image":["res"]} Commented el 31 ene. 2019 a las 19:43
  • bueno en el array json que pongo arriba esta "json_metadata": esto no lo puedo iterar :/ Commented el 31 ene. 2019 a las 19:45
  • ¿Cómo estás tratando de iterarlo? primero debes convertirlo en un objeto JS. Para eso puedes hacer lo siguiente. Supongamos que myjson = res[0] contiene el resultado que esperas y supongamos que tu clave json_metadata contiene el string metadata que deseas iterar. Puedes hacer lo siguiente para convertir ese string en un JSON: myjson.json_metadata = JSON.parse(myjson.json_metadata);. Ahora tu clave json_metadata contiene un objeto JS que podrás iterar. Comenta los resultados. Commented el 2 feb. 2019 a las 14:51

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.