я пытаюсь настроить API с помощью node.js, теперь я пытаюсь добавить async / await в мой код, и по какой-то причине мой визуальный код начинает отображать некоторые ошибки, распознавая попытку как ключевые слова, более того, я использую почтальонвыполнять запросы к моему API, и я получаю ошибки, возвращенные мне.Я пытался выяснить, что случилось, и я не могу найти это.Вот мой продукт-контроллер, где мой get отображает ошибки:
//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
