如何在NodeJS Express路由器res.render中传递Mongoose返回变量?

时间:2017-02-21 15:42:44

标签: javascript node.js mongodb express mongoose

我已经设置了一个基本的Express NodeJS服务器。

我想将从mongoose.find()返回的设置值保存为res.render作为变量,但每次它都会向我发出错误Cannot read property of undefined

这是我的routes.js文件。

var Setting = require('../../models/setting');

module.exports = function(app, passport) {
 app.get('/admin', function(req, res) {
    Setting.find(function(err, setting) {
        if (err) {
            return res.send(500, err);
        }
        console.log(setting);
        res.render('admin/customize/settings', {
            title: 'Change Setting | eduBird',
            user: req.user,
            setting: res.setting
                // setting: res.settings
        });
    });

});

这是我的设置模式:

var mongoose = require('mongoose');

var settingSchemma = mongoose.Schema({
 logo: {
    logo16: String,
    logo32: String,
    logo96: String,
    logo128: String,
    logo128white: String
 }
});

module.exports = mongoose.model('Setting', settingSchemma);

以下是浏览器中的错误,我正在使用pug视图。

name='logo32' value=setting.logo.logo32) Cannot read property 'logo' of undefined

这是我的终端中的日志,请注意console.log(setting)正在执行设置,因为我只有一个徽标文档。

[nodemon] starting `node ./bin/www server`
  Server running on port3000
  [ { _id: 58ac45xxxx3xxxxf3,
   __v: 0,
  logo:
 { logo128white: 'https://res.cloudinary.com/xxxxx/image/uplo
   ad/xxxxx/128x128-white_aqya8e.png',
   logo128: 'https://res.cloudinary.com/xxxxx/image/upload/v1
   xxxxxxx/128x128_jn9dzz.png',
   logo96: '',
   logo32: 'https://res.cloudinary.com/xxxx/image/upload/v14
    xxxxx/32x32_wtk3gk.png',
   logo16: 'https://res.cloudinary.com/pinterested222/image/upload/v14
     xxxx/16x16_o4gzhd.png' } } ]
  GET /stylesheets/style.css 304 6.947 ms - -

以下是快照Snapshot of the error

更新

以下是我的.pug文件的片段,我正在渲染,所有缩进都是正确的。

           form.logoForm.z-depth-3.col.s12(method='post' action='/settings/logo')
                .row
                    .col.s12.m8.offset-m2
                        h4.center Change Logo Settings
                        p.center Add URL for valid Logos.
                        .divider
                    .input-field.col.s12
                        input.validate#logo16(placeholder="URL for 16x16 .png log" name='logo16' value=setting.logo.logo16)
                        label.active(for='logo16') 16x16 Logo
                    .input-field.col.s12
                        input.validate#logo32(placeholder="URL for 32x32 .png log" name='logo32' value=setting.logo.logo32)
                        label.active(for='logo32') 32x32 Logo
                    .input-field.col.s12
                        input.validate#logo128(placeholder="URL for 128x128 .png log" name='logo128' value=setting.logo.logo128)
                        label.active(for='logo128') 128x128 Logo
                    .input-field.col.s12
                        input.validate#logo128white(placeholder="URL for 128x128 white .png log" name='logo128white' value=setting.logo.logo128)
                        label.active(for='logo128white') 128x128 Logo White(for dark bg)
                .row
                    .col.s12.m4.offset-m4.l4.offset-l4
                        button.center.logoSubmit.btn.btn-large.indigo.darken-4.white-text.waves-effect.waves-light.disabled Save #[i.fa.fa-chevron-circle-right.right]
                        button.center.btn.btn-large.indigo.darken-4.white-text.waves-effect.waves-light Save #[i.fa.fa-chevron-circle-right.right]
                    .col.s12.m6.offset-m3
                        p.center(style='margin-top: .3em; padding: .2em') *You can only save the settings if any of the form fields are different/updated.

1 个答案:

答案 0 :(得分:1)

您的代码中存在拼写错误,分配setting: res.setting是导致问题的原因。此外, find() 会返回一系列文档,而不是单个文档。您可能希望使用 findOne()

var Setting = require('../../models/setting');

module.exports = function(app, passport) {
    app.get('/admin', function(req, res) {
        Setting.findOne({}).exec(function(err, setting) {
            if (err) {
                return res.send(500, err);
            }
            console.log(setting);
            res.render('admin/customize/settings', {
                title: 'Change Setting | eduBird',
                user: req.user,
                setting: setting                    
            });
        });
    });
}