【问题标题】:Node.js ReferenceError: Product is not definedNode.js ReferenceError:产品未定义
【发布时间】:2019-05-10 09:27:20
【问题描述】:

我尝试使用 node.js 设置 API,现在我尝试将 async/await 添加到我的代码中,由于某种原因,我的可视化代码 lint 开始显示一些错误,将 try 识别为关键字,此外,我正在使用邮递员执行对我的 API 的请求并且我收到错误返回给我。我一直试图找出问题所在,但我似乎无法找到它。 这是我的产品控制器,我得到的是显示错误:

//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'
        });
    }
};

我的产品库

    '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);
}

返回给我的错误列表

[错误列表]https://imgur.com/a/AqXsaz21

【问题讨论】:

    标签: javascript node.js rest api


    【解决方案1】:

    我认为您遇到了两个不同的错误。 要修复 JS 提示错误,您可以查看以下链接:

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

    Does JSHint support async/await?

    但是对于您的Product is not defined 错误,我可以在您的product-repository 的第3 行看到您有一个名为product 的变量,我相信一旦您将该变量更改为Product,您的错误就会得到解决。

    【讨论】: