Reputation: 3
I'm new to keystone js. Right now I'm making a web app that has user dashboard. I'm trying to use the method of updateItem upon a user item, which already been indexed / searched / queried in the first parameter of the function.
// KEYSTONE METHOD
User.updateItem({_id: req.params.id}, req.body, {
fields: 'user',
flashErrors: true,
logErrors: true},
function(err) {
if (err) {
console.log("An error happened!");
console.log(err);
} else {
return res.redirect('/dashboard/' + req.params.id);
}
next();
});
Error popped up in console, shows:
{
error: 'invalid configuration',
detail: 'Invalid path specified in fields to update [user] for list User'
}
It seems that it doesn't found the user model. I wanted to console.log if the system found the user model through callbacks params, but this method doesn't have one.
As I said, I'm really new in keystonejs and I'm not even close to understanding it fully of how things really work here. But, I feel like it's really close to work...
** CODE REF : Dashboard views **
var keystone = require('keystone');
var user = keystone.list("User");
var User = keystone.List("User");
exports = module.exports = function (req, res) {
var view = new keystone.View(req, res);
var locals = res.locals;
// Init locals
locals.currentUser = req.body || {};
view.query('currentUser', user.model.findById(req.params.id));
view.on('post', { action: 'user.update' }, function(next) {
// KEYSTONE METHOD
User.updateItem({_id: req.params.id}, req.body, {
fields: 'user',
flashErrors: true,
logErrors: true},
function(err) {
if (err) {
console.log("An error happened!");
console.log(err);
} else {
return res.redirect('/dashboard/' + req.params.id);
}
next();
});
});
// Render the view
view.render('../dashboard/dashboard-main');
};
** CODE REF : Jade code **
form(method="post")
input(type='hidden', name='action', value='user.update')
.row.mb-4
.col-md-4
label First Name
input.form-control(type="text" name="first" value=currentUser.name.first)
.col-md-4
label Last Name
input.form-control(type="text" name="last" value=currentUser.name.last)
.row.mb-10
.col-md-12
button.btn.btn-primary(type="submit")
i.fa.fa-fw.fa-lg.fa-check-circle
| Save
Here's the User model:
var keystone = require('keystone');
var Types = keystone.Field.Types;
/**
* User Model
* ==========
*/
var User = new keystone.List('User');
User.add({
name: { type: Types.Name, required: true, index: true },
email: { type: Types.Email, initial: true, required: true, unique: true, index: true },
password: { type: Types.Password, initial: true, required: true },
}, 'Permissions', {
isAdmin: { type: Boolean, label: 'Can access Keystone', index: true },
});
// Provide access to Keystone
User.schema.virtual('canAccessKeystone').get(function () {
return this.isAdmin;
});
/**
* Relationships
*/
User.relationship({ ref: 'Post', path: 'posts', refPath: 'author' });
/**
* Registration
*/
User.defaultColumns = 'name, email, isAdmin';
User.register();
Note: I've used yeoman generator for this app, and just added new route file, and jade view file for the user dashboard.
Upvotes: 0
Views: 514
Reputation: 168
var User = keystone.List("User");
is invalid in your view / route.js. You only use the capitalized .List when you are creating a new List in your model, as it is a class initiation, like you have in your model.
Therefore the correct context for your dashboard view is likely to be:
var keystone = require('keystone');
var user = keystone.list("User");
/* var User = keystone.List("User"); */
exports = module.exports = function (req, res) {
var view = new keystone.View(req, res);
var locals = res.locals;
// Init locals
locals.currentUser = req.body || {};
view.query('currentUser', user.model.findById(req.params.id));
view.on('post', { action: 'user.update' }, function(next) {
// KEYSTONE
/* User.updateItem({_id: req.params.id}, req.body, { */
user.updateItem({_id: req.params.id}, req.body, {
fields: 'user',
flashErrors: true,
logErrors: true},
function(err) {
if (err) {
console.log("An error happened!");
console.log(err);
} else {
return res.redirect('/dashboard/' + req.params.id);
}
next();
});
});
// Render the view
view.render('../dashboard/dashboard-main');
Upvotes: 2