【问题标题】:Output nested array with nr id in front of bracket输出嵌套数组,nr id 在括号前
【发布时间】:2021-01-25 13:37:11
【问题描述】:

我是这个东西的新手,我在问如何从 MySQL 表中执行这个嵌套数组的示例,以输出像 node js express API 的 javascript 中的聊天示例一样,我使用的是 vue-cli。

查看chats Mysql 表: Mysql chat table

这就是我想要达到的目标:

 chats: {
 SENDER ID -> 1: {
          isPinned: true,
          msg: [
            { // this is me (sender)
              textContent: 'How can we help? We\'re here for you!',
              time: 'Mon Dec 10 2018 07:45:00 GMT+0000 (GMT)',
              isSent: true,
              isSeen: true
            },
            { // this is the receiver
              textContent: 'Hey John, I am looking for the best admin template. Could you please help me to find it out?',
              time: 'Mon Dec 10 2018 07:45:23 GMT+0000 (GMT)',
              isSent: false,
              isSeen: true
            } 
          ]
        },
        2: {
          isPinned: false,
          msg: [
            {
              textContent: 'Hi',
              time: 'Mon Dec 10 2018 07:45:00 GMT+0000 (GMT)',
              isSent: true,
              isSeen: true
            },
            {
              textContent: 'Hello. How can I help You?',
              time: 'Mon Dec 11 2018 07:45:15 GMT+0000 (GMT)',
              isSent: false,
              isSeen: true
            } 
          ]
        }
      }

这是我到目前为止所做的: mysql查询:

getChats: callBack => {
    pool.query(
      'SELECT * FROM chat WHERE reciever_userid=1 AND sender_userid=5 OR sender_userid=1 AND reciever_userid=5  ORDER BY time',
      [],
      (error, chats) => {
        if (error) {
          callBack(error)
        }
        return callBack(null, chats)
      }
    )
  }

API 调用:

 getChats((err, chatz) => {
  if (err) {
    console.log(err)
    return
  }   

  const chats = chatz.map(el => { 
    el.msg = {
      textContent    : el.textContent   || null,
      time           : el.time          || null,
      isSent         : el.isSent        || null,
      isSeen         : el.isSeen        || null
    } 
    delete el['status'] 
    delete el['sender_userid'] 
    delete el['reciever_userid'] 
    delete el['textContent'] 
    delete el['time'] 
    delete el['isSent'] 
    delete el['isSeen'] 
    delete el['id'] 
    return el
  }).flat()
    
  return res.json({
    chats
  })
})

邮递员的结果不是我在上面的例子中寻找的。​​p>

    {
    "chats": [
        {
            "isPinned": "true",
            "msg": {
                "textContent": "heyyy",
                "time": "2020-10-07T15:04:40.000Z",
                "isSent": "true",
                "isSeen": "true"
            }
        },
        {
            "isPinned": "true",
            "msg": {
                "textContent": "yesss",
                "time": "2020-10-07T15:04:40.000Z",
                "isSent": "true",
                "isSeen": "true"
            }
        }
    ]
}

【问题讨论】:

  • 所以问题是您将聊天作为数组而不是带有索引的对象?或者那个味精是一个对象而不是一个数组?还是两者兼而有之?
  • 我不知道像示例中那样输出热输出 1: { 在前面
  • 好的,看看我的回答

标签: javascript mysql node.js arrays nested


【解决方案1】:

要匹配您的目标格式,请将“Api call”代码更改为:

getChats((err, chatz) => {
    if (err) {
      console.log(err)
      return
    }   
  
    const chatsArray = chatz.map(el => { 
      el.msg = {
        textContent    : el.textContent   || null,
        time           : el.time          || null,
        isSent         : el.isSent        || null,
        isSeen         : el.isSeen        || null
      } 
      delete el['status'] 
      delete el['sender_userid'] 
      delete el['reciever_userid'] 
      delete el['textContent'] 
      delete el['time'] 
      delete el['isSent'] 
      delete el['isSeen'] 
      delete el['id'] 
      return el
    }).flat()

    let chats = {};
    chatsArray.forEach((chat, i) => chats[i + 1] = chat)
      
    return res.json({
      chats
    })
  })

【讨论】:

  • 感谢您的宝贵时间!现在我得到了正确的结果。
  • 还有一件事,我从您的代码结果中看到它们是分开的所有 textContent,来自 id 1 和 id 2 或 2 到 1 的所有 textContent 应该作为一个,就像在 achive 示例中一样,有可能实现吗?
猜你喜欢
  • 2021-01-27
  • 2010-11-27
  • 2017-07-18
  • 2022-01-25
  • 2020-06-19
  • 2019-08-11
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多