2017-01-23 92 views
0

我有MongoDB數據庫文件其中包含數組奇怪MongoDB的響應格式

{ 
    "_id":ObjectId("588116a66f7d758b144177f7"), 
    "name":"Michael", 
    "email":"[email protected]", 
    "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 
    }); 
}); 
+0

你嘗試'VAR totalHours = req.user.shifts [7]。加入( '')'? –

+0

你正在使用嵌套數組錯誤。嵌套數組應該包含對象。 – Tom

+0

謝謝,@JyothiBabuAraja它說,req.user.shifts [7] .join不是一個函數,我應該單獨編寫函數嗎? – LiveLongCandy51

回答

0

我認爲這個問題是您正在使用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]; 

},函數(ERR){ 的console.log( 「ERR」,ERR)});

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

是HTML格式的總小時只打印{{totalHours}}