xnok
xnok

Reputation: 339

Node.js ReferenceError: Product is not defined

im trying to setup an API using node.js, now i trying to add async/await to my code, and for some reason my visual code lint started to display some errors, recognizing try as keywords, moreover, im using postman to perform requests to my API and im getting errors returned to me. I've been trying to figure out what was wrong, and i can't seem to find it. Here's my product-controller, where my get is displaying errors:

//const mongoose = require('mongoose');
//const Product = mongoose.model('Product');
const Product = require('../models/Product')
const ValidationContract = require('../validators/fluent-validator');
const repository = require('../repositories/product-repository');


exports.get = async(req, res, next) => {
    try {
        var data = await repository.get();
        res.status(200).send(data);
    } catch (e) {
        res.status(500).send({
            message: 'Falha ao processar sua requisição'
        });
    }
}
exports.getBySlug = async(req, res, next) => {
    try {
        var data = await repository.getBySlug(req.params.slug);
        res.status(200).send(data);
    } catch (e) {
        res.status(500).send({
            message: 'Falha ao processar sua requisição'
        });
    }
}

exports.getById = async(req, res, next) => {
    try {
        var data = await repository.getById(req.params.id);
        res.status(200).send(data);
    } catch (e) {
        res.status(500).send({
            message: 'Falha ao processar sua requisição'
        });
    }
}


exports.getByTag = async(req, res, next) => {
    try {
        const data = await repository.getByTag(req.params.tag);
        res.status(200).send(data);
    } catch (e) {
        res.status(500).send({
            message: 'Falha ao processar sua requisição'
        });
    }
}


exports.post = async(req, res, next) => {
    let contract = new ValidationContract();
    contract.hasMinLen(req.body.title, 3, 'o título deve conter pelo menos 3 caracteres');
    contract.hasMinLen(req.body.slug, 3, 'o slug deve conter pelo menos 3 caracteres');
    contract.hasMinLen(req.body.description, 3, 'a descrição deve conter pelo menos 3 caracteres');
    if (!contract.isValid()) {
        res.status(400).send(contract.errors()).end();
        return;
    }
    try{
        await repository.create(req.body)
        res.status(201).send({
            message: 'Produto cadastrado com sucesso!'
        });
    } catch (e){
        res.status(500).send({
            message: 'Falha ao processar sua requisição'
        });
    }
};
exports.put = async(req, res, next) => {
    try {
        await repository.update(req.params.id, req.body);
        res.status(200).send({
            message: 'Produto atualizado com sucesso!'
        });
    } catch (e) {
        res.status(500).send({
            message: 'Falha ao processar sua requisição'
        });
    }
};
exports.delete = async(req, res, next) => {
    try {
        await repository.delete(req.body.id)
        res.status(200).send({
            message: 'Produto removido com sucesso!'
        });
    } catch (e) {
        res.status(500).send({
            message: 'Falha ao processar sua requisição'
        });
    }
};

my product-repository

    'use strict';
const mongoose = require('mongoose');
const product = mongoose.model('Product');

exports.get = async() => {
    const res = await Product.find({
        active: true
    }, 'title price slug');
    return res;
}
exports.getBySlug = async(slug) => {
    const res = await Product
        .findOne({
            slug: slug,
            active: true
        }, 'title description price slug tags');
    return res;
}
exports.getById = async(id) => {
    const res = await Product
        .findById(id);
    return res;
}
exports.getByTag = async(tag) => {
    const res = Product
        .find({
            tags: tag,
            active: true
        }, 'title description price slug tags');
    return res;
}
exports.create = async(data) => {
    var product = new Product(data);
    await product.save();
}

exports.update = async(id, data) => {
    await Product
        .findByIdAndUpdate(id, {
            $set: {
                title: data.title,
                description: data.description,
                price: data.price,
                slug: data.slug
            }
        });
}
exports.delete = async(id) => {
    await Product
        .findOneAndRemove(id);
}

List of errors returned to me

[Error list]https://i.sstatic.net/bLYRA.jpg1

Error 1 Error 2 Error 3 Error 4 Error 5

Upvotes: 0

Views: 2252

Answers (1)

Pedram marandi
Pedram marandi

Reputation: 1614

I think you are experiencing two different errors. To fix the JS hint error you may have a look to the below link:

JSHint does not recognise Async/Await syntax in Visual Studio Code (VSCode)

Does JSHint support async/await?

But for your Product is not defined error I can see in line number 3 of your product-repository you have a variable called product and I believe your error will be fixed once you change that variable to Product instead.

Upvotes: 2

Related Questions