Tutorial: Build a Basic CRUD App with Node.js

Tutorial: Build a Basic CRUD App with Node.js

Learn how to securely store, update, and display user data in a simple Node.js / Express.js app.

Pedro Mira Vaz

First of all, THANK YOU SO MUCH! My first go with node.js, this has been very interesting. Two notes:

1) There’s a comma missing in blog.js, line 11 (before the token property).
2) I get an error running this:

TypeError: Cannot read property ‘findAll’ of undefined
at router.get (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\routes\blog.js:27:15)
at Layer.handle [as handle_request] (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\index.js:174:3)
at router (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\index.js:275:10)
at app.use (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\app.js:55:12)
at Layer.handle [as handle_request] (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\index.js:335:12)
at Immediate.next (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\index.js:275:10)
at Immediate.<anonymous> (C:\Users\Pedro\Google Drive (pessoal)\Pedro\WindowsApps\CRUDnode\blog\node_modules\express\lib\router\index.js:635:15)

Ömer Faruk ERKUL

I did everything in tutorial but when I start server I get this message "Cannot read property ‘findAll’ of undefined"
What’s the solution of this problem?

Edit: TypeError: Cannot read property ‘findAll’ of undefined
at router.get (C:\Users…\blog\routes\blog.js:27:15)
at Layer.handle [as handle_request] (C:\Users…\blog\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users…\blog\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users…\blog\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users…\blog\node_modules\express\lib\router\layer.js:95:5)
at C:\Users…\blog\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users…\blog\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users…\blog\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Users…\blog\node_modules\express\lib\router\index.js:174:3)
at router (C:\Users…\blog\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (C:\Users…\blog\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users…\blog\node_modules\express\lib\router\index.js:317:13)
at C:\Users…\blog\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users…\blog\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users…\blog\node_modules\express\lib\router\index.js:275:10)
at client.getUser.then.user (C:\Users…\blog\app.js:62:7)
at process._tickCallback (internal/process/next_tick.js:68:7)

Pedro Mira Vaz

I had exactly the same output

Matt Raible

Thanks for the heads up Pedro. You’re right that a comma was missing. The following was missing from the bottom of models.js too, which causes the findAll issue you saw.

module.exports = { Post };

I’ve updated this tutorial with the fixes and it should work great now!

Matt Raible

Hello Ömer: there was a line missing from the end of models.js that caused this issue:

module.exports = { Post };

I’ve updated this tutorial with the fix. Can you please try again and see if it works for you?

Ömer Faruk ERKUL

Thank you for Reply! it works perfectly from now on.

abasiono udosen

this is my own error, any help with this

Error: secret option required for sessions
at session (/home/abaz/Documents/ngHP/blog/node_modules/express-session/index.js:199:12)
at Layer.handle [as handle_request] (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:317:13)
at /home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:335:12)
at next (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:275:10)
at SendStream.error (/home/abaz/Documents/ngHP/blog/node_modules/serve-static/index.js:121:7)
at emitOne (events.js:116:13)
at SendStream.emit (events.js:211:7)
at SendStream.error (/home/abaz/Documents/ngHP/blog/node_modules/send/index.js:270:17)
at SendStream.onStatError (/home/abaz/Documents/ngHP/blog/node_modules/send/index.js:421:12)
at next (/home/abaz/Documents/ngHP/blog/node_modules/send/index.js:764:28)
at /home/abaz/Documents/ngHP/blog/node_modules/send/index.js:772:23
at FSReqWrap.oncomplete (fs.js:152:21)

abasiono udosen

help with this

Unknown authentication strategy "oidc"
Error: Unknown authentication strategy “oidc”
at attempt (/home/abaz/Documents/ngHP/blog/node_modules/passport/lib/middleware/authenticate.js:173:37)
at authenticate (/home/abaz/Documents/ngHP/blog/node_modules/passport/lib/middleware/authenticate.js:349:7)
at /home/abaz/Documents/ngHP/blog/node_modules/@okta/oidc-middleware/src/connectUtil.js:79:28
at Layer.handle [as handle_request] (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:317:13)
at /home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:335:12)
at next (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:275:10)
at urlencodedParser (/home/abaz/Documents/ngHP/blog/node_modules/body-parser/lib/types/urlencoded.js:91:7)
at Layer.handle [as handle_request] (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:317:13)
at /home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:335:12)
at next (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:275:10)
at SessionStrategy.strategy.pass (/home/abaz/Documents/ngHP/blog/node_modules/passport/lib/middleware/authenticate.js:325:9)
at SessionStrategy.authenticate (/home/abaz/Documents/ngHP/blog/node_modules/passport/lib/strategies/session.js:71:10)
at attempt (/home/abaz/Documents/ngHP/blog/node_modules/passport/lib/middleware/authenticate.js:348:16)
at authenticate (/home/abaz/Documents/ngHP/blog/node_modules/passport/lib/middleware/authenticate.js:349:7)
at Layer.handle [as handle_request] (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:317:13)
at /home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:335:12)

Randall Degges

Hey, you must have forgotten to set the secret option when initializing the session middleware, eg:

app.use(session({
secret: “{aLongRandomString}”,
resave: true,
saveUninitialized: false
}));

abasiono udosen

and i now got this…

Unknown authentication strategy "oidc"
Error: Unknown authentication strategy “oidc”
at attempt (/home/abaz/Documents/ngHP/blog/node_modules/passport/lib/middleware/authenticate.js:173:37)
at authenticate (/home/abaz/Documents/ngHP/blog/node_modules/passport/lib/middleware/authenticate.js:349:7)
at /home/abaz/Documents/ngHP/blog/node_modules/@okta/oidc-middleware/src/connectUtil.js:79:28
at Layer.handle [as handle_request] (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:317:13)
at /home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:335:12)
at next (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:275:10)
at urlencodedParser (/home/abaz/Documents/ngHP/blog/node_modules/body-parser/lib/types/urlencoded.js:91:7)
at Layer.handle [as handle_request] (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:317:13)
at /home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:335:12)
at next (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:275:10)
at SessionStrategy.strategy.pass (/home/abaz/Documents/ngHP/blog/node_modules/passport/lib/middleware/authenticate.js:325:9)
at SessionStrategy.authenticate (/home/abaz/Documents/ngHP/blog/node_modules/passport/lib/strategies/session.js:71:10)
at attempt (/home/abaz/Documents/ngHP/blog/node_modules/passport/lib/middleware/authenticate.js:348:16)
at authenticate (/home/abaz/Documents/ngHP/blog/node_modules/passport/lib/middleware/authenticate.js:349:7)
at Layer.handle [as handle_request] (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:317:13)
at /home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/abaz/Documents/ngHP/blog/node_modules/express/lib/router/index.js:335:12)

Michael Owolabi

I can see that all the fixes has been added, however for me the "unknown authentication strategy “oidc” " error persists.

Below is the error as shown in the screenshot below: https://uploads.disquscdn.c…

Matt Raible

Hello Michael: can you please compare your code with the example project on GitHub. I’m certain this tutorial works since I completed it myself. It’s been six weeks since I tried it, so you might ensure you have the same dependencies in package.json.

Shivam

Hi Matt, I am getting similar kind of issue, I have used updated code:

TypeError: D:\Shivam\Node\blog\views\index.pug:14
12| .posts
13| ul
> 14| each post in posts
15| .row
16| .offset-sm-2.col-sm-8
17| li

Cannot read property ‘length’ of undefined
at eval (eval at wrap (D:\Shivam\Node\blog\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:80:32)
at eval (eval at wrap (D:\Shivam\Node\blog\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:123:4)
at template (eval at wrap (D:\Shivam\Node\blog\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:143:97)
at Object.exports.renderFile (D:\Shivam\Node\blog\node_modules\pug\lib\index.js:418:38)
at Object.exports.renderFile (D:\Shivam\Node\blog\node_modules\pug\lib\index.js:408:21)
at View.exports.__express [as engine] (D:\Shivam\Node\blog\node_modules\pug\lib\index.js:455:11)
at View.render (D:\Shivam\Node\blog\node_modules\express\lib\view.js:135:8)
at tryRender (D:\Shivam\Node\blog\node_modules\express\lib\application.js:640:10)
at Function.render (D:\Shivam\Node\blog\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (D:\Shivam\Node\blog\node_modules\express\lib\response.js:1008:7)

Pavan

I have the same error whats the issue?

Mike Miller

Interesting post, thanks… We are planning to implement EBS Integration with Okta, Any recommendations pls…

Matt Raible

Hello Mike. You might have better luck posting you question to our Okta Developer forums. That’s where the bulk of our developer community resides.

Heba Elayoty

Thank You! It is really useful and saved a lot of time

Heba Elayoty

Could you please add a sample for adding list of checkbox options in DB?

Mike Miller

Thanks for your suggestion.