簡體   English   中英

奇怪的mongodb響應格式

[英]Strange mongodb response format

我有一個包含數組的mongodb數據庫文檔

{
   "_id":ObjectId("588116a66f7d758b144177f7"),
   "name":"Michael",
   "email":"mick@shift.com",
   "company":"Shift",
   "username":"MichaelP",
   "password":"$2a$10$unH/7anPylxk1x5zk6/so.YcByZqQoBdFi9IlWnHty2gNouUrt7ea",
   "position":"Manager",
   "shifts":[
      "day",
      "day",
      null,
      "night",
      null,
      null,
      null,
      "15"
   ],
   "score":[
      null,
      "550",
      null
   ],
   "__v":0
}

我想在我的網頁上打印出shifts[7] 我是node.js的新手,但可以正常打印{{username}}{{company}}但是shifts[7]打印為'{ '0': '1', '1': '5' }'我以為它是index.js中的一個簡單解決方案,但是我似乎無法弄清楚。

請在下面添加我的index.js代碼的簡化版本,並感謝您的幫助。

router.get('/hub',ensureAuthenticated, function(req, res, next){
  var collection = db.collection('users');
  var totalHours= req.user.shifts[7] + '';
  var totalhour=0;

  res.render('hub', {username: req.user.username, 
    company: req.user.company, 
    position: req.user.position, 
    totalHours: totalhour 
  });
});

我認為問題是您正在使用req.user而不是用戶響應,並且您沒有得到單個對象來響應,因此,發生這種情況,您應該傳遞_id以獲取單個用戶值,然后將其用於下一步。

router.get('/hub/:id',ensureAuthenticated, function(req, res, next){
var collection = Restangular.One('users',req.user._id);
    collection.get().then(function(err,user){
    var totalHours= users.shifts[7] + '';
    var totalhour=0;
    $scope.user=user;
    $scope.totalHours = user.shift[7];

},function(err){console.log(“ err”,err)});

res.render('hub', {username: req.user.username, company:req.user.company, position: req.user.position, totalHours: totalhour})
});

用html表示的總小時數,只需打印{{totalHours}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM