简体   繁体   English

Node.JS追加数组并转换为JSON

[英]Node.JS Append Array and Convert to JSON

I have the below that runs a few functions to grab different data and i am trying to create/append to an array to "create" a new array that i can then JSON.stringify and pass to the Jade view, i seem to only be able to get the "k" variable of the two loops it does and outputs as "coin1coin2" with the "" instead of a JSON array with the balance and addresses (multiples) after each coin name. 我下面有运行一些函数以捕获不同的数据,并且我试图创建/追加到数组以“创建”一个新数组,然后我可以将其JSON.stringify并传递到Jade视图,我似乎只是能够获得两个循环的“ k”变量,并输出为带有“”的“ coin1coin2”,而不是每个硬币名称后带有余额和地址(多个)的JSON数组。

// list all wallets //
app.get('/wallet', function(req, res){
    var coins = app.get('coins');
    var addresses = "";
    for (var k in coins) {
        addresses += k;
        console.log('Coin: ' + k);
        //console.log(k+' => '+coins[k]);
        connect(coins[k]);
        client.getBalance('44abc0a80102', 6, function(err, balance) {
            if (err) return console.log(err);
            addresses += balance;
            //console.log('Balance: ' + balance);
        });
        client.getAddressesByAccount('44abc0a80102', function(err, address) {
            if (err) return console.log(err);
            addresses["address"] += address;
        });
    }
    console.log(addresses);
    res.render('wallet', {
        title: 'Wallet',
        address: JSON.stringify(addresses)
    });
});

In the Jade view i am just doing p= address to show the full array so i can at least see what i am getting before creating logic in the view to loop through it. 在Jade视图中,我只是在做p= address来显示完整的数组,所以在视图中创建逻辑来遍历它之前,我至少可以看到我得到的东西。

The problem here is that your for loop is synchronous, whereas your client.getBalance and client.getAddressesByAccount calls are (almost definitely) not. 这里的问题是您的for循环是同步的,而您的client.getBalanceclient.getAddressesByAccount调用(几乎肯定)不是同步的。

When your for loop finishes, your client.getBalance and client.getAddressesByAccount operations are still in progress, so they haven't had a chance to populate your addresses thing yet. for循环完成时, client.getBalanceclient.getAddressesByAccount操作仍在进行中,因此它们还没有机会填充addresses Thus it is empty when it's passed to the templating system. 因此,当它传递到模板系统时为空。

You probably want to use something like async 's map method to do this instead. 您可能想要使用asyncmap方法之类的方法来代替它。 It'd look something like this: 看起来像这样:

// list all wallets
app.get('/wallet', function(req, res){
    // array? object?
    var coins = app.get('coins');

    async.map(coins, function(coinId, cb) {
        // ???
        console.log('Coin: ' + JSON.stringify(coin));

        // not sure what this does!
        connect(coin);

        client.getBalance('44abc0a80102', 6, function(err, balance) {
            if (err) return cb(err);

            client.getAddressesByAccount('44abc0a80102', function(err, address) {
                if (err) return cb(err);

                return cb(null, {
                    address: address,
                    balance: balance,
                });
            });
        });
    }, function(err, addresses) {
        // note that we only act on an error here
        if (err) {
            return console.log(err);
        }

        // [{address: ..., balance: ...}, ...]
        console.log(addresses);

        res.render('wallet', {
            title: 'Wallet',
            address: JSON.stringify(addresses),
        });
    });
});

The addresses value will be an array of {address: ..., balance: ...} objects. addresses值将是{address: ..., balance: ...}对象的数组。 I'm not sure what you were expecting it to be, since at one point you treat it like a string, then you treat it like an object. 我不确定您的期望是什么,因为在某一时刻您将其视为字符串,然后将其视为对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM