Back

Technologies:

javascriptjavascript
avatar
Tolerim
23 days ago

The view directory does not contain the "list" view.

After working with my code for a long time, it suddenly gave me an error while browsing:

Error: Failed to lookup the "list" view in the "C:\Users\anieksn emmanuel\documents\todolist-v2-starting-files\views" views directory.

Here's my code:

const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const app = express();
app.set('view engine', 'ejs');
const path = require("path");
app.set("views",  path.join(dirname + '/views'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
mongoose.connect('mongodb://127.0.0.1:27017/todolistDB')
.then(() => console.log('Connected!'));
const itemsSchema = new mongoose.Schema({
name: String
});
const Item = new mongoose.model ("Item", itemsSchema);
const item1 = new Item({
name: "Welcome to your Todolist"
});
const item2 = new Item({
name: "Hit the + button to add a new item."
});
const item3 = new Item({
name: "<-- Click the check box to delete an item."
});
const defaultItems = [item1, item2, item3];
async function getItems() {
const Items = await Item.find({});
return Items;
}
app.get("/", function(req, res) {
getItems().then(function(FoundItems){
  if (FoundItems.length === 0){
    Item.insertMany(defaultItems).then(function(){
        console.log("Data inserted") // Success
    }).catch(function(error){
        console.log(error) // Failure
    });
    res.redirect("/");
  } else{
    var path = require("path")
    res.render("list", {listTitle: "Today", newListItems: FoundItems});
  }
});
});
app.post("/", function(req, res){
const itemName = req.body.newItem;
const item = new Item({
name: itemName
});
item.save();
res.redirect("/")
});
app.get("/work", function(req, res){
res.render("list", {listTitle: "Work List", newListItems: workItems});
});
app.get("/about", function(req, res){
res.render("about");
});
app.listen(3000, function() {
console.log("Server started on port 3000");
});
Answers(2)
avatar
Vogi
22 days ago
Verified Answer
The error message you are seeing is due to the fact that the "list" view cannot be found in the "C:\Users\anieksn emmanuel\documents\todolist-v2-starting-files\views" views directory. To fix this error, you need to ensure that the list file is in a directory called views and you need to add it to the app. You can do this by adding the following code snippet to your app:
app.set ('views', path.join (__dirname, 'views'));
This error is telling you that it cannot find the file called list. You can find more information on how to resolve this error on stackoverflow. I hope this helps!

Sources

How to resolve "Failed to lookup view "list" in views directory".?Error: Failed to lookup view "list" in views directoryFailed to lookup view "list" in views directory - Stack OverflowHow to resolve SharePoint list view threshold error: “The view cannot ...
avatar
Tolerim
23 days ago
The error message you are seeing seems to imply that the view engine cannot find the "list" view in the views directory specified. Make sure that you have a file named "list.ejs" in the views directory you have set in your app using app.set("views", ...). Double check that the file name is spelled correctly and that it has the extension ".ejs".
;