非循环JSON返回循环JSON错误

时间:2019-03-19 22:42:15

标签: node.js json express

我正在将GET请求发送到路由,应该以JSON响应,但返回的错误为Converting circular structure to JSON。我尝试过投影所需的字段,但这并不能防止出现此错误。我要返回的对象都不返回自己。当我返回对象之一的值时,它会返回正常值。

这是路线的代码:

router.get('/', auth.required, (req, res, next) => {
  const email = jwt.verify(req.cookies.dbmtg_jwt, 'secret', function (err, decoded) {
    if (err) {
      throw err
    }
    return decoded.email
  })
  const user = User.findOne({'email': email}, function (err, user) {
    if (err) {
      throw err
    }
    console.log(user)
  }).select({'_id': 0, 'hash': 0, 'salt': 0, '__v': 0})
  return res.json(user)
})

我尝试返回的user JSON不是循环的。是{ email: 'myemail@gmail.com', cardCollection: [], decks: [] }。即使我没有投影我想要的JSON响应字段(如下所示),我仍然会收到该错误。

{ _id: 5c905ca89b61bb3f6f556d06,
  email: 'myemail@gmail.com',
  cardCollection: [],
  decks: [],
  salt: 'a3ff9ec1a7f76a018a7608c98f0be53e',
  hash: '62c2f3a1797be8b397d151e50b0d72d19355348c1f39006b841185d7e34fa63cb701d429d3417fe5c717db6305075916eab63fe3062fb16d730f9f3bf0c31a2a16d8313f1d733946666536f528449f61f529b0864fa2cce70e1496f02d93d0b5999c5bdbba432ddecf544ac240cfd66d9a6c4e9daad0b73dce482dd6d35cba81d201d90805920f50537508eee6f500b35ec3ebd158beb98e25f7d082b2df97f9ec954bd5455314c4191d4b60d78b5ab83d6a5c4c5d97e537ff9b2e38a5a2ee4a159beb0d2a0454cef3fca72b7233766782c41401ceb621e70cdb289c15b1efbc0ea83b7a0db4dd72507af46f9eb774d33026f03fe1f290c0f91d9c8a24d090ef69fbf22b03052c68798a016564c1cc14a6c8b9cd8a3752149b04e7caa08e9cb497601e467251a11bb8dc874c2b2afc7c56318a541560db851e6ee649b9c22e43eedf14d4feaede561ebeba1336cfeb5ee127c14e1ed7a32760875e2ccce0c7a481c9cccdfb1c1733c632c547615f5fdbc6817c191ae20a45bce21d6744490cc7a108b53350ede160e92b3777fcb148294a1ed31eb870fbd0336fc9b2a90f4b89432b72533711cef7ba258ac5083d565007973f14f555623ee693f3c0955e303b113afc018d9e9920bc18673bb2e2e9bee6f1769bb99adcfc3c4a3b13d172843f065ad7aa3067ec0a27bb7fa17684908e3c8d3c46ab0eee04c5479df0eead4fc6',
  __v: 0 }

1 个答案:

答案 0 :(得分:0)

  • <?php $result = $conn->query("SELECT * FROM user_data WHERE advert_type = 'Deluxe'"); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $p_id = $row['user_id']; $new_array[] = $p_id; } } echo '<script>'; echo 'var imagesArray = ' . json_encode($new_array) . ';'; echo '</script>'; ?> <script> //Good shuffle algorithm: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } function changeComponent() { // store image query in a variable var $allImages = $(".premium_ad_img"); // take X random IDs by shuffling the list and taking the first X // (slice total dynamically, in case you ever need more or less than exactly 6) var randomIDs = shuffle(imagesArray).slice(0, $allImages.length); // iterate over each image, using the index of the iteration. $allImages.each(function (idx) { $(this).attr('src', 'data/profiles/users/profile_img/' + randomIDs[idx] + '/main.jpg'); $(this).parent(".premium_component_link").attr('href', 'profile.php?p_id=' + randomIDs[idx]); }); } $(document).ready(function () { changeComponent(); setInterval(function() { changeComponent(); }, 5000); }) </script> 没有保存从数据库返回的实际用户
  • 请尝试使用在回调 {strong> const user
  • 中返回的user,而不是从对User.find()的调用中返回user

function(err, user) {}