Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Retrieving Entries from a Table

findAll is not a function.

Hello,

I tried to implement this in an application I'm building myself but seemed to have run into an issue. I'm trying to retrieve all vacancies in my database. There is 1 in there.

In my routing file, I have:

    const Vacancy = require("../models/vacancy");

// GET /vacancies
app.route("/vacancies").get((req, res) => {
    Vacancy.findAll().then(vacancies => {
        res.render("vacancies", { vacancies: vacancies });
    });
});

In my model file:

"use strict";

module.exports = (sequelize, DataTypes) => {
    const Vacancy = sequelize.define("Vacancy", {
        title: DataTypes.STRING,
        description: DataTypes.STRING,
        releaseDate: DataTypes.DATE,
        isActive: DataTypes.BOOLEAN,
        weWant: DataTypes.STRING,
        weSearch: DataTypes.STRING,
        applicationEmail: DataTypes.STRING,
    }, {});

    Vacancy.associate = models => {
        Vacancy.belongsTo(models.Company, {
            foreignKey: {
                name: "companyId",
            },
        });

        Vacancy.belongsTo(models.STA_JobType, {
            foreignKey: {
                name: "jobTypeId",
            },
        });

        Vacancy.belongsTo(models.STA_Industry, {
            foreignKey: {
                name: "industryId",
            },
        });

        Vacancy.belongsTo(models.STA_ExperienceRange, {
            foreignKey: {
                name: "experienceRangeId",
            },
        });

        Vacancy.belongsTo(models.STA_Workspace, {
            foreignKey: {
                name: "workspaceId",
            },
        });

        Vacancy.belongsToMany(models.Location, {
            through: "VacancyLocation",
            foreignKey: {
                name: "locationId",
            },
        });
    };

    return Vacancy;
};

When going to the route, I receive this error:

   {"err":"TypeError occured. See the stack for more information.","stack":["TypeError: Vacancy.findAll is not a function","    at app.route.get (C:\\ST\\CJ\\server\\routes\\index.js:39:11)"," ...

Might you have a clue?

Thanks.

Joseph Wasden
Joseph Wasden
20,406 Points

In your routing file, is your require statement closed properly? in your shared snippet, it looks like you left off the closing bits.

    const Vacancy = require("../models/vaca  // notice here the lack of closing quotes or parentheses

// GET /vacancies
app.route("/vacancies").get((req, res) => {
    Vacancy.findAll().then(vacancies => {
        res.render("vacancies", { vacancies: vacancies });
    });
});

Joseph Wasden Sorry, mate. Unfortunately that's a copy paste mistake into here. :D It's actually like this in my file:

const Vacancy = require("../models/vacancy");

1 Answer

Valeshan Naidoo
Valeshan Naidoo
27,008 Points

You probably found a solution already, but could it be that your require statement does not have the Vacancy method appended to it? i.e.

const Vacancy = require("../models/vacancy").Vacancy;

Since findAll() is a built in ORM classMethod, it should work with your implementation.

I'm just going by what we've seen so far with this course, in there we have

var Article = require("../models").Article;