nodejs循环数组来创建对象

时间:2018-06-13 16:09:51

标签: arrays node.js each

我尝试为数组中的每个元素创建一个

html元素但是它不起作用你能帮助我吗?

我的节点js代码:

def announce_highest(who, previous_high=0, previous_score=0):
    """Return a commentary function that announces when WHO's score
    increases by more than ever before in the game.

    >>> f0 = announce_highest(1) # Only announce Player 1 score gains
    >>> f1 = f0(11, 0)
    >>> f2 = f1(11, 1)
    1 point! That's the biggest gain yet for Player 1
    >>> f3 = f2(20, 1)
    >>> f4 = f3(5, 20) # Player 1 gets 4 points, then Swine Swap applies
    19 points! That's the biggest gain yet for Player 1
    >>> f5 = f4(20, 40) # Player 0 gets 35 points, then Swine Swap applies
    20 points! That's the biggest gain yet for Player 1
    >>> f6 = f5(20, 55) # Player 1 gets 15 points; not enough for a new high
    """
    assert who == 0 or who == 1, 'The who argument should indicate a player.'
    # BEGIN PROBLEM 7
    gain = #compare previous argument to current argument
    if gain > previous_high:
        if gain == 1:
            print(gain, 'point! That\'s the biggest gain yet for Player', who)
        else: 
            print(gain, 'points! That\'s the biggest gain yet for Player', who)

    return announce_highest(some args)
    # END PROBLEM 7

此代码如下所示:con.connect(function(err) { // if (err) throw err; console.log("Connected!") con.query( 'SELECT * FROM `commentairesapplicationscommunautaire` ', function (error, results, fields) { for (var i = 0; i < results.length; i++){ var zeuzryueartt = results[i].contenu console.log(zeuzryueartt) res.end('<p> '+ zeuzryueartt +' </p>') } }); });

但我的案子错了

由于

1 个答案:

答案 0 :(得分:1)

您将在第一步结束响应流。一旦构建完整的响应html,您只需要调用end

con.connect(function(err) {
    // if (err) throw err;
    console.log("Connected!")
    con.query( 'SELECT * FROM `commentairesapplicationscommunautaire` ', function (error, results, fields) {
       const entireHTML = results.map(result => `<p>${result.contenu}</p>`).join('')
       res.end(entireHTML)
    });
});