NodeJS:如何将数据放入视图中?

NodeJS : How to get data into views?

如何从视图中的模型获取单个数据?

这是我的控制器:

router.get('/edit/:id', isAuthenticated, function(req, res)
    {
        File.find({_id:req.params.id}, function(err, files){

            if(err) 
                return console.error(err.stack);

            if(!files) 
                return console.error("File not found!");

            res.render('edit', { user: req.user, files: files});

        });

    });

你看,我根据他的ID取了一个唯一的数据

在我看来,我得到的数据(文件)是这样的:

each file in files
            div.row
                div.col-sm-6.col-md-6
                    form(action='/edit', class='form-upload', enctype='multipart/form-data', method='post')
                        .form-group
                            label(class='control-label') Position
                            input(type='text', name='order', class='form-control input-lg', value='#{file.order}')
                        .form-group
                            label(class='control-label') Nom du fichier
                            input(type='text', name='name', class='form-control input-lg', value='#{file.name}')
                        .form-group
                            label(class='control-label') Délai
                            input(type='text', name='delay', class='form-control input-lg', value='#{file.delay}')

问题:如何避免 each file in files 并直接调用 files.name 例如?

如果您希望您的模型 return 对象而不是数组,请改用 findOne

router.get('/edit/:id', isAuthenticated, function(req, res) {
    File.findOne({
        _id: req.params.id
    }, function(err, file) {
        if (err)
            return console.error(err.stack);
        if (!file)
            return console.error("File not found!");
        res.render('edit', {
            user: req.user,
            file: file
        });
    });
});

补充一下,element(class="className")可以缩短为element.className

.row
    .col-sm-6.col-md-6
        form.form-upload(action='/edit', enctype='multipart/form-data', method='post')
            .form-group
                label.control-label Position
                input.form-control.input-lg(type='text', name='order', value='#{file.order}')
            .form-group
                label.control-label Nom du fichier
                input.form-control.input-lg(type='text', name='name', value='#{file.name}')
            .form-group
                label.control-label Délai
                input.form-control.input-lg(type='text', name='delay', value='#{file.delay}')