无法将对象推送到数组

时间:2020-01-31 21:15:31

标签: node.js

我正在尝试让服务器获取请求,并向用户发送一个数组,其中包含所有为其机器人投票的用户(来自不和谐)。

    app.get("/api/bot/vote/:id", async (req, res) => {
  let botId = req.params.id;
  let auth = req.headers.authorization;

  if (!auth)
    return res.json({
      success: "false",
      error: "Authorization header not found."
    });


  // if (!bot.auth) return res.json({ "success": "false", "error": "Create a bot authorization token." });
  //if (bot.auth !== auth) return res.json({ "success": "false", "error": "Incorrect authorization token." });
  if (botId) {
    database.ref(`Bots/${botId}`).once("value", async function(snapshot) {
      if (snapshot.val() === null)
        return res.json({ success: "false", error: "Bot not found." });
      if (snapshot.val().apiKey != auth) {
        return res.json({
          success: "false",
          error: "Incorrect authorization token."
        });
      } else {
      database.ref("/Users").once("value",function(data) {
           const user_vote = [];
        data.forEach(function(snap) {
          database.ref(`Users/${snap.key}/votes/${botId}`).once("value",function(vote) {
          if(vote.val() === null) return
            user_vote.push(snap.key)


          })

        })
        return res.json({
          success: "true",
          Vote_Users_id: user_vote,
        });
      })

      }
    });
  }
});

当我尝试使用推送并执行console.log(user)时,它将与每个对该机器人投票的用户一起发送数组。但是当我尝试将其发送给用户时,他得到了一个空数组[]

0 个答案:

没有答案