Keystone Tutorial: cannot read property 'id' of undefined - node.js

I was following the tutorial on starting KeystoneJS from scratch. But when I got to the second part of the tutorial, creating data models, I got this error:
Error thrown for request: /keystone/
TypeError: Cannot read property 'id' of undefined
at IndexRoute (/root/websie/node_modules/keystone/admin/server/routes/index.js:39:16)
at Layer.handle [as handle_request] (/root/websie/node_modules/express/lib/router/layer.js:95:5)
at next (/root/websie/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/root/websie/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/root/websie/node_modules/express/lib/router/layer.js:95:5)
at /root/websie/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/root/websie/node_modules/express/lib/router/index.js:335:12)
at next (/root/websie/node_modules/express/lib/router/index.js:275:10)
at /root/websie/node_modules/keystone/admin/server/app/createDynamicRouter.js:26:3
at Layer.handle [as handle_request] (/root/websie/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/root/websie/node_modules/express/lib/router/index.js:317:13)
at /root/websie/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/root/websie/node_modules/express/lib/router/index.js:335:12)
at next (/root/websie/node_modules/express/lib/router/index.js:275:10)
at handleUploadedFiles (/root/websie/node_modules/keystone/lib/uploads.js:6:54)
at Layer.handle [as handle_request] (/root/websie/node_modules/express/lib/router/layer.js:95:5)
GET /keystone/ 500 8.835 ms
Since I'm new to Keystone JS, I don't quite know if this problem is with the code itself or the database.
Things to consider:
This is run on a linux environment
MongoDB is freshly installed
These are all the files so far:
keystone.js
const keystone = require('keystone')
keystone.init({
'cookie secret': 'SECRET KEY',
'name' : 'theproject',
'user model' : 'User',
'auth' : 'true',
'auto update' : 'true',
});
keystone.import('models');
keystone.start();
User.js:
const keystone = require('keystone');
var User = new keystone.List('User');
User.add({
displayName: { type: String },
password: { type: keystone.Field.Types.Password },
email: { type: keystone.Field.Types.Email, unique: true },
})
User.schema.virtual('canAccessKeystone').get(function () {
return true;
});
User.defaultColumns = 'id, displayName, email';
User.register();

Your User model doesn't have an "id" attribute. So when you set your User.defaultColumns to "id" it doesn't find anything.
All models do however have an "_id" attribute.
I would just remove "id" from your default columns and move forward.
User.defaultColumns = 'displayName, email';
If that doesn't fix it you might be requesting the id elsewhere.

Related

Cast to string failed for value "[ 'Electrician', 'Active' ]" at path "work" for model "User"

Error logs:
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
Server running on port 5000
CastError: Cast to string failed for value "[ 'Electrician', 'Active' ]" at path "work" for
model "User"
at model.Query.exec (C:\Users\Admin\Desktop\New Horizon\node_modules\mongoose\lib\query.js:4437:21)
at model.Query.Query.find (C:\Users\Admin\Desktop\New Horizon\node_modules\mongoose\lib\query.js:2050:8)
at Function.find (C:\Users\Admin\Desktop\New Horizon\node_modules\mongoose\lib\model.js:2070:13)
at C:\Users\Admin\Desktop\New Horizon\app.js:415:12
at Layer.handle [as handle_request] (C:\Users\Admin\Desktop\New Horizon\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Admin\Desktop\New Horizon\node_modules\express\lib\router\route.js:137:13)
at ensureAuthenticated (C:\Users\Admin\Desktop\New Horizon\app.js:19:14)
at Layer.handle [as handle_request] (C:\Users\Admin\Desktop\New Horizon\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\Admin\Desktop\New Horizon\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Admin\Desktop\New Horizon\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\Admin\Desktop\New Horizon\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Admin\Desktop\New Horizon\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\Users\Admin\Desktop\New Horizon\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Admin\Desktop\New Horizon\node_modules\express\lib\router\index.js:275:10) at methodOverride (C:\Users\Admin\Desktop\New Horizon\node_modules\method-override\index.js:65:14)
at Layer.handle [as handle_request] (C:\Users\Admin\Desktop\New Horizon\node_modules\express\lib\router\layer.js:95:5) {
messageFormat: undefined, stringValue: `"[ 'Electrician', 'Active' ]"`,
kind: 'string', value: [ 'Electrician', 'Active' ],
path: 'work', reason: null
}
The code :
app.get('/search',ensureAuthenticated,(req,res)=>{
try {
User.find({$or:[{work:{'$regex':req.query.dsearch}},
{status:{'$regex':req.query.dsearch}}
]},(err,user)=>{
if(err){
// req.flash('error', 'Employee not founded .')
console.log(err);
console.log('Finding book');
}
else{
// req.flash('success_msg', 'Finding Employee ... ');
res.render('result',{user:user});
// console.log(user);
}
})
}
catch (error) {
console.log(error);
}
});

strange behavior jsonwebtoken expired error

I have a Node.js application which uses jsonwebtoken for session management.
However, after the token expired, when I want to access again, I got:
{ TokenExpiredError: jwt expired
at Object.module.exports [as verify] (/home/ubuntu/me-n-you/node_modules/jsonwebtoken/verify.js:126:19)
at auth (/home/ubuntu/me-n-you/app_server/routes/index.js:14:9)
at Layer.handle [as handle_request] (/home/ubuntu/me-n-you/node_modules/express/lib/router/layer.js:95:5)
at next (/home/ubuntu/me-n-you/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/ubuntu/me-n-you/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/ubuntu/me-n-you/node_modules/express/lib/router/layer.js:95:5)
at /home/ubuntu/me-n-you/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/ubuntu/me-n-you/node_modules/express/lib/router/index.js:335:12)
at next (/home/ubuntu/me-n-you/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/home/ubuntu/me-n-you/node_modules/express/lib/router/index.js:174:3)
at router (/home/ubuntu/me-n-you/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/home/ubuntu/me-n-you/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/ubuntu/me-n-you/node_modules/express/lib/router/index.js:317:13)
at /home/ubuntu/me-n-you/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/ubuntu/me-n-you/node_modules/express/lib/router/index.js:335:12)
at next (/home/ubuntu/me-n-you/node_modules/express/lib/router/index.js:275:10)
name: 'TokenExpiredError',
message: 'jwt expired',
expiredAt: 2018-05-08T21:14:27.000Z }
I repeat to get the same errors until a very long time then I could access again.
But, I can access if using another device or browser at the same time.
I have no idea how to fix it. Does anyone have any clue on this?
Thanks in advance.
Richard Xu
You can again logged in ,also if you are using bcrypt
bcrypt.compare(req.body.password, data[0].password, (err, resposne) => {
if (err) {
res.status(500).json(err);
}
//response is either true or false
if (resposne) {
const token = jwt.sign({
email: data[0].email,
userId: data[0]._id
}, "secret",
{
expiresIn: "1h"
})
return res.status(200).json({
message: 'Auth successful',
token: token
})
} else {
return res.status(401).json({ message: 'Auth failed' })
}
})
Increase your expiry limit

How to setup passport-identityserver3

I am new with Node-red and passport-identityserver3.
I am having some difficulties how to setup the passport.
I installed the passport using the C:\Users\xxx>npm install passport-identityserver3 command and try to set the C:\Users\xxx\settings.js file:
adminAuth: {
type:"strategy",
strategy: {
name: "passport-identityserver3",
label: 'Sign in with',
icon:"an icon",
strategy: require('passport-identityserver3').Strategy,
options: {
configuration_endpoint: 'http://localhost:xxxx/.well-known/openid-configuration',
client_id: 'my_client_id',
client_secret: 'my_client_secret',
callback_url: 'http://localhost:1880/auth/strategy/callback',
scopes: ['profile', 'offline_access'],
},
verify: function(token, tokenSecret, profile, done) {
done(null, profile);
}
},
users: [
{ username: "admin",permissions: ["*"]}
]
},
I am getting the following error:
http://127.0.0.1:1880/auth/strategy
Error: Unknown authentication strategy "passport-identityserver3"
at attempt (C:\Users\xxxx\AppData\Roaming\npm\node_modules\node-
red\node_modules\passport\lib\middleware\authenticate.js:173:37)
at authenticate (C:\Users\xxxx\AppData\Roaming\npm\node_modules\node-
red\node_modules\passport\lib\middleware\authenticate.js:349:7)
at Layer.handle [as handle_request]
(C:\Users\xxxx\AppData\Roaming\npm\node_modules\node-
red\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\xxxx\AppData\Roaming\npm\node_modules\node-
red\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\xxxx\AppData\Roaming\npm\node_modules\node-
red\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request]
(C:\Users\xxxx\AppData\Roaming\npm\node_modules\node-
red\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\xxxx\AppData\Roaming\npm\node_modules\node-
red\node_modules\express\lib\router\index.js:281:22
at Function.process_params
(C:\Users\xxxx\AppData\Roaming\npm\node_modules\node-
red\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\xxxx\AppData\Roaming\npm\node_modules\node-
red\node_modules\express\lib\router\index.js:275:10)
at SessionStrategy.strategy.pass
(C:\Users\xxxx\AppData\Roaming\npm\node_modules\node-
red\node_modules\passport\lib\middleware\authenticate.js:325:9)
Thanks you.
You need to run the npm i passport-identityserver3 in the Node-RED User Directory. By default this should be:
C:\Users\xxx\.node-red
not
C:\Users\xxx\
The settings.js file should also be in this directory. When you start Node-RED it will list which User Directory and settings.js it is using

Nodejs middleware .pre shows not a function

var categoryList = new Referral({categoryList : category});
categoryList.pre('save', function (next) {
Referral.find({categoryList : category}, function (err, docs) {
if (!docs.length){
next();
}else{
console.log('Data exists: ', category);
next(new Error("Data exists!"));
}
})
})
Referral is my variable assigned to my schema.
categoryList is the object
This gives an error
TypeError: categoryList.pre is not a function at
D:\Aventyn\ClipCare_v2\app\api.js:112:18 at Layer.handle [as
handle_request]
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\layer.js:95:5)
at next
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request]
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\layer.js:95:5)
at
D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:277:22
at Function.process_params
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:330:12)
at next
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:271:10)
at Function.handle
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:176:3)
at router
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:46:12)
at Layer.handle [as handle_request]
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:312:13)
at
D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:280:7
at Function.process_params
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:330:12)
at next
(D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:271:10)
at
D:\Aventyn\ClipCare_v2\node_modules\express\lib\router\index.js:618:15
Try changing:
categoryList.pre('save', function (next) {
// ...
})
to:
categoryList.schema.pre('save', function (next) {
// ...
})
The .pre() is a method of the Mongoose schema, not a model.
Middleware (like pre hooks) are part of a schema. It looks like you're trying to use it on a single document, which is not how it works.
Instead, use it on the schema that you used to create the Referral model:
ReferralSchema.pre('save', ...);
This does mean that the pre hook will be applied to all documents of that schema.

Cannot read property 'prototype' of undefined

I'm starting with Parse and Node.js. I'm trying to retrieve data from Parse db and I don't manage to.
var Parse = require('parse').Parse;
Parse.initialize("*************", "*****************");
module.exports = {
index: function(req, res, next){
var query = new Parse.Query(Parse.Event);
query.find({
success: function(results) {
console.log(results);
},
error: function(error) {
console.log(error.message);
}
});
}
}
And I got this error :
TypeError: Cannot read property 'prototype' of undefined
at Object.Parse.Query (/Users/tchiss_a/test/webapp/node_modules/parse/build/parse-latest.js:7771:33)
at module.exports.index (/Users/tchiss_a/test/webapp/api/Events.js:7:20)
at Layer.handle [as handle_request] (/Users/tchiss_a/test/webapp/node_modules/express/lib/router/layer.js:82:5)
at next (/Users/tchiss_a/test/webapp/node_modules/express/lib/router/route.js:100:13)
at Route.dispatch (/Users/tchiss_a/test/webapp/node_modules/express/lib/router/route.js:81:3)
at Layer.handle [as handle_request] (/Users/tchiss_a/test/webapp/node_modules/express/lib/router/layer.js:82:5)
at /Users/tchiss_a/test/webapp/node_modules/express/lib/router/index.js:235:24
at Function.proto.process_params (/Users/tchiss_a/test/webapp/node_modules/express/lib/router/index.js:313:12)
at /Users/tchiss_a/test/webapp/node_modules/express/lib/router/index.js:229:12
at Function.match_layer (/Users/tchiss_a/test/webapp/node_modules/express/lib/router/index.js:296:3)
Can someone help me please ?
There is a fix listed in the comments, but the actual problem here is that you're treating a user-defined class "Event" like a Parse class.
change
var query = new Parse.Query(Parse.Event);
to
var query = new Parse.Query("Event");

Resources