Reputation: 9282
I had set a basic Express NodeJS server.
I want to save the value of the setting returned from mongoose.find() to res.render as a variable, but every time it's throwing me an error that Cannot read property of undefined
.
Here is my routes.js file.
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
});
});
});
This is my setting model:
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);
Here is the error in the browser, I am using pug view.
name='logo32' value=setting.logo.logo32) Cannot read property 'logo' of undefined
Here is my the log in my terminal, notice that console.log(setting)
is executing the settings right, since I have just one document for logo.
[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 - -
Here is a snapshot: Snapshot of the error
UPDATE
Here is the snippet of my .pug file, I am rendering, all indentations are right.
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.
Upvotes: 1
Views: 754
Reputation: 103365
You have a typo in your code, the assignment setting: res.setting
is the one causing the issue. Also, find()
returns an array of docs, not a single document. You may want to use findOne()
for that:
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
});
});
});
}
Upvotes: 1