my localhost page shows error No default engine - ejs

I was trying to run my app throw nodemon locally but the page keep loading and shows nothing
the list.ejs file
const express = require("express");
const bodyparser = require("body-parser");
const app = express();
app.set('view engine','ejs');
var day = "";
app.get("/",function(req,res){
var today = new Date();
if(today.getDay()===5 || today.getDay()===6){
day = "weekend";
} else {
day = "weekday";
}
app.render("list",{kindofday:day});
})
app.listen(5000,function(){
console.log("server is running on 5000");
})
error message :
Error: No default engine was specified and no extension was provided.
at new View (C:\Users\faisa\Web Development\toollist-v1\node_modules\express\lib\view.js:61:11)
at Function.render (C:\Users\faisa\Web Development\toollist-v1\node_modules\express\lib\application.js:570:12)
at C:\Users\faisa\Web Development\toollist-v1\app.js:13:9
at Layer.handle [as handle_request] (C:\Users\faisa\Web Development\toollist-v1\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\faisa\Web Development\toollist-v1\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\faisa\Web Development\toollist-v1\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\faisa\Web Development\toollist-v1\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\faisa\Web Development\toollist-v1\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\faisa\Web Development\toollist-v1\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\faisa\Web Development\toollist-v1\node_modules\express\lib\router\index.js:275:10)

you need to set the view directory
app.set("views", path.join(__dirname, "views"));
replace views with the name of your directory

Related

Express JS TypeError: Cannot read properties of undefined (reading 'id')

I am trying to build a database query with express js to get a recipe from a table that joins a recipe's information with specific ingredients from a third table.
The query I have works fine with pgadmin and postman when I am not specifying the id from my recipes table on which the join occurs but when I try and specify the recipe.id to return just one recipe and ingredients I get this error:
TypeError: Cannot read properties of undefined (reading 'id')
at /Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/index.js:112:25
at Layer.handle [as handle_request] (/Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/route.js:144:13)
at Route.dispatch (/Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/route.js:114:3)
at Layer.handle [as handle_request] (/Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/layer.js:95:5)
at /Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/index.js:284:15
at Function.process_params (/Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/index.js:346:12)
at next (/Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/index.js:280:10)
at /Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/index.js:30:3
at Layer.handle [as handle_request] (/Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/layer.js:95:5)
Here is the express js query:
app.get('/getjoinedrecipes', (req, res) => {
console.log(req, res)
pool
.query(
'select * from ingredientrecipes inner join recipes on ingredientrecipes.recipe_id = recipes.id inner join ingredients on ingredientrecipes.ingredient_id = ingredients.id',
[req.body.recipes.id]
)
.then((result) => {
res.status(200).send(result.rows)
})
.catch((error) => {
console.log(error)
res.status(500).send(error)
})
})
Here is the body I am sending to http://localhost:3001/getjoinedrecipes via postman for testing:
{
"id": 8
}

Mongodb Atlas Express.js Error: getaddrinfo ENOTFOUND

I want to connect my express app to my mongoDb Atlas cluster.I'm from Iran, and cloud databases are sanctioned for us. I used VPN to bypass it in order to be able to practice.
Is there some coding mistake that I've done or is it because of using VPN?
The error:
(node:9008) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
connected
events.js:174
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND
is listening...
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
Emitted 'error' event at:
at GetAddrInfoReqWrap.doListen [as callback] (net.js:1448:12)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:17)
[nodemon] app crashed - waiting for file changes before starting...
And the code:
database.js
----------------
const mongoDb = require('mongodb');
const MongoClient = mongoDb.MongoClient;
let _db;
const mongoConnect = callback => {
MongoClient.connect(
'mongodb+srv://<someUser>:<somePassword>#<someCluster>-zh1eb.mongodb.net/test?retryWrites=true'
)
.then(client => {
console.log('\nconnected\n');
_db = client.db();
callback();
})
.catch(err => {
console.log('\nerror\n', err);
throw err;
});
};
const getDb = () => {
if (_db) {
return _db;
}
throw 'NO DATABASE FOUND';
};
exports.mongoConnect = mongoConnect;
exports.getDb = getDb;
app.js
------------
...
const mongoConnect = require('./util/database').mongoConnect;
...
mongoConnect(() => {
app.listen(3000, '\nis listening...\n');
});
I found out what was the problem:
mongoConnect(() => {
app.listen(3000, '\nis listening...\n');
});
should actually be
mongoConnect(() => {
app.listen(3000, () => { console.log('\nis listening...\n');});
});
Silly me... :)
For those having such issue:
first check out whether the source of problem is your code or the connection to MongoDb.
That is instead of choosing "connect your application" select "connect with Mongodb Compass".
If the Mongodb Compass could connect and show your cluster, then definitely there is something wrong with your code, esp. how it's coded to connect.

Asking for account linking in Google Actions v2 fails "ReferenceError: SignIn is not defined"

I'm migrating the code over and i can't get my account linking sign in process to work. After looking at the documentation and code i don't see any error in the way i'm calling SignIn().
I've also made sure to update action.json and add the v2 account linking stuff in there, and my app always had account linking working so the account linking section in the Console is up to date. I'm using Action SDK and not Dialogflow.
const {actionssdk} = require('actions-on-google');
const app = actionssdk({debug: true});
app.intent('actions.intent.MAIN', conv => {
conv.ask(new SignIn())
})
app.intent('actions.intent.SIGN_IN', (conv, input, signin) => {
if (signin.status === 'OK') {
const access = conv.user.access.token // possibly do something with access token
conv.ask('Great, thanks for signing in! What do you want to do next?')
} else {
conv.ask(`I won't be able to save your data, but what do you want to do next?`)
}
})
module.exports.assistant = app;
Logs:
ReferenceError: SignIn is not defined
at app.intent.conv (/usr/local/lucida/web/client-apps/src/routes/google_home.js:4:16)
at Function.<anonymous> (/usr/local/lucida/web/client-apps/node_modules/actions-on-google/dist/service/actionssdk/actionssdk.js:138:23)
at next (native)
at /usr/local/lucida/web/client-apps/node_modules/actions-on-google/dist/service/actionssdk/actionssdk.js:22:71
at __awaiter (/usr/local/lucida/web/client-apps/node_modules/actions-on-google/dist/service/actionssdk/actionssdk.js:18:12)
at Function.handler (/usr/local/lucida/web/client-apps/node_modules/actions-on-google/dist/service/actionssdk/actionssdk.js:85:16)
at Object.<anonymous> (/usr/local/lucida/web/client-apps/node_modules/actions-on-google/dist/assistant.js:55:32)
at next (native)
at /usr/local/lucida/web/client-apps/node_modules/actions-on-google/dist/assistant.js:22:71
at __awaiter (/usr/local/lucida/web/client-apps/node_modules/actions-on-google/dist/assistant.js:18:12)
at standard (/usr/local/lucida/web/client-apps/node_modules/actions-on-google/dist/assistant.js:51:41)
at /usr/local/lucida/web/client-apps/node_modules/actions-on-google/dist/framework/express.js:23:13
at omni (/usr/local/lucida/web/client-apps/node_modules/actions-on-google/dist/assistant.js:44:53)
at Layer.handle [as handle_request] (/usr/local/lucida/web/client-apps/node_modules/express/lib/router/layer.js:95:5)
at next (/usr/local/lucida/web/client-apps/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/usr/local/lucida/web/client-apps/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/usr/local/lucida/web/client-apps/node_modules/express/lib/router/layer.js:95:5)
at /usr/local/lucida/web/client-apps/node_modules/express/lib/router/index.js:281:22
at param (/usr/local/lucida/web/client-apps/node_modules/express/lib/router/index.js:354:14)
at param (/usr/local/lucida/web/client-apps/node_modules/express/lib/router/index.js:365:14)
at Function.process_params (/usr/local/lucida/web/client-apps/node_modules/express/lib/router/index.js:410:3)
at next (/usr/local/lucida/web/client-apps/node_modules/express/lib/router/index.js:275:10)
You should include SignIn when you import the library:
const {actionssdk, SignIn} = require('actions-on-google');

TypeError: Cannot read property 'text' of undefined

router.post('/addtodo', (req, res, next) => {
let todo = new Todos({
text: req.body.text,
date: new Date(),
});
Todos.addTodo(todo, (err, todos, next) => {
if(err) throw err;
res.json(todos);
});
});
I am trying to save a document in the Todo schema using the above method
Schema and methods are as follows
const todoSchema = mongoose.Schema({
text: {
type: String
},
date: {
type: Date,
default: Date.now
}
});
const Todos = module.exports = mongoose.model('todos', todoSchema);
//Retrieve documents
module.exports.getTodos = (callback) => {
Todos.find(callback);
};
//Add document
module.exports.addTodo = (todo, callback) => {
Todos.create(todo, callback);
};
But when I try to POST a json object using postman it shows this error.
<body>
<pre>TypeError: Cannot read property 'text' of undefined
<br> at router.post (C:\Users\shahaji.shinde\Desktop\practice\todo-mean\router\todoroute.js:18:24)
<br> at Layer.handle [as handle_request] (C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\router\layer.js:95:5)
<br> at next (C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\router\route.js:137:13)
<br> at Route.dispatch (C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\router\route.js:112:3)
<br> at Layer.handle [as handle_request] (C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\router\layer.js:95:5)
<br> at C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\router\index.js:281:22
<br> at Function.process_params (C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\router\index.js:335:12)
<br> at next (C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\router\index.js:275:10)
<br> at Function.handle (C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\router\index.js:174:3)
<br> at router (C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\router\index.js:47:12)
<br> at Layer.handle [as handle_request] (C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\router\layer.js:95:5)
<br> at trim_prefix (C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\router\index.js:317:13)
<br> at C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\router\index.js:284:7
<br> at Function.process_params (C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\router\index.js:335:12)
<br> at next (C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\router\index.js:275:10)
<br> at expressInit (C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node_modules\express\lib\middleware\init.js:40:5)
</pre>
</body>
The error is in the first code snippet- line number 3.
Two possible causes come to my mind with the information you posted:
- It can be because you are not sending the appropriate Content-Type: application/json (check it).
- Server is not ready to parser json:
For that, you should have something like this:
var bodyparser = require('body-parser');
app.use(bodyparser.urlencoded({ extended: true }));
app.use(bodyparser.json({limit: '10mb'}));
Let me know if it helps you

Passportjs and Postgres TypeError using sequelizejs

I am trying to authenticate using passportjs/sequelize but keep getting a typeError here :
TypeError: undefined is not a function
at module.exports.getUser (/Users/ra/Desktop/jb/controllers/users.js:44:12)
at Layer.handle [as handle_request] (/Users/ra/Desktop/jb/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/ra/Desktop/jb/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Users/ra/Desktop/jb/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/ra/Desktop/jb/node_modules/express/lib/router/layer.js:95:5)
at /Users/ra/Desktop/jb/node_modules/express/lib/router/index.js:277:22
at param (/Users/ra/Desktop/jb/node_modules/express/lib/router/index.js:349:14)
at param (/Users/ra/Desktop/jb/node_modules/express/lib/router/index.js:365:14)
at Function.process_params (/Users/ra/Desktop/jb/node_modules/express/lib/router/index.js:410:3)
at next (/Users/ra/Desktop/jb/node_modules/express/lib/router/index.js:271:10)
at Function.handle (/Users/ra/Desktop/jb/node_modules/express/lib/router/index.js:176:3)
at router (/Users/ra/Desktop/jb/node_modules/express/lib/router/index.js:46:12)
at Layer.handle [as handle_request] (/Users/raDesktop/jb/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/ra/Desktop/jb/node_modules/express/lib/router/index.js:312:13)
at /Users/ra/Desktop/jb/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/ra/Desktop/jb/node_modules/express/lib/router/index.js:330:12)
Here is what the controller looks like:
getUser: function(req, res) {
User.find({
where: {
username: req.params.id,
displayName: req.user.displayName
}
}).success(function(User) {
res.send(User);
});
},
This is the get request from passport after authentication
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
failureRedirect: '/login'
}),
function(req, res) {
console.log(req.user.username)
res.cookie('signIn', 'true');
res.redirect('/api/users/' + req.user.username);
}
);