繁体   English   中英

Node.js ReferenceError:产品未定义

[英]Node.js ReferenceError: Product is not defined

我试图使用node.js设置API,现在我试图向代码中添加async / await,由于某种原因,我的可视代码棉绒开始显示一些错误,将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/AqXsaz2 1

错误1 错误2 错误3 错误4 错误5

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

JSHint无法识别Visual Studio Code(VSCode)中的Async / Await语法

JSHint是否支持异步/等待?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM