const express = require('express');
const authMiddleware = require(’…/middlewares/auth’);
const Project = require(’…/models/Project’);
const Task = require(’…/models/Task’);
const router = express.Router();
router.use(authMiddleware);
router.get(’/’, async (req, res) => {
try {
const projects = await Project.find().populate([‘user’, ‘tasks’]);
return res.send({ projects });
} catch (err) {
return res.status(400).send({ error: ‘Erro ao carregar o projeto’ });
}
});
router.get(’/:projectId’, async (req, res) => {
try {
const project = await Project.findById(req.params.projectId).populate([‘user’, ‘tasks’]);
return res.send({ project });
} catch (err) {
return res.status(400).send({ error: ‘Error loading project’ });
}
});
router.post(’/’, async (req, res) => {
try {
const { title, description, tasks } = req.body;
const project = await Project.create({ title, description, user: req.userId });
await Promise.all(tasks.map(async task => {
const projectTask = new Task({ ...task, project: project._id });
await projectTask.save();
project.tasks.push(projectTask);
}));
await project.save();
return res.send({ project });
} catch (err) {
console.log(err)
return res.status(400).send({ error: ‘Erro ao criar o projeto’ });
}
});
router.put(’/:projectId’, async (req, res) => {
try {
const { title, description, tasks } = req.body;
const project = await Project.findByIdAndUpdate(req.params.projectId, {
title,
description
}, { new: true });
project.tasks = [];
await Task.remove({ project: project._id });
await Promise.all(tasks.map(async task => {
const projectTask = new Task({ ...task, project: project._id });
await projectTask.save();
project.tasks.push(projectTask);
}));
await project.save();
return res.send({ project });
} catch (err) {
return res.status(400).send({ error: ‘Error updating project’ });
}
});
router.delete(’/:projectId’, async (req, res) => {
try {
await Project.findByIdAndRemove(req.params.projectId);
return res.send();
} catch (err) {
return res.status(400).send({ error: ‘Error deleting project’ });
}
});
module.exports = app => app.use(’/projects’, router);
[2/4] Fetching packages…
[3/4] Linking dependencies…
[4/4] Building fresh packages…
success Saved lockfile.
Done in 30.90s.
PS C:\Users\paula\projetos\apiNode\src> yarn add mongoose
yarn add v1.3.2
[1/4] Resolving packages…
[2/4] Fetching packages…
[3/4] Linking dependencies…
[4/4] Building fresh packages…
success Saved lockfile.
success Saved 0 new dependencies.
Done in 3.15s.
PS C:\Users\paula\projetos\apiNode\src> node index.js
{ MongoError: Unknown modifier: $pushAll
at Function.MongoError.create (C:\Users\paula\projetos\apiNode\node_modules\mongodb-core\lib\error.js:31:11)
at toError (C:\Users\paula\projetos\apiNode\node_modules\mongodb\lib\utils.js:139:22)
at C:\Users\paula\projetos\apiNode\node_modules\mongodb\lib\collection.js:1059:67
at C:\Users\paula\projetos\apiNode\node_modules\mongodb-core\lib\connection\pool.js:469:18
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: ‘MongoError’,
message: ‘Unknown modifier: $pushAll’,
driver: true,
index: 0,
code: 9,