2017-01-23 85 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}}