Eu estou conciliando minhas aulas do Alura com uma que eu comprei da Udemy. Porém, os professores de lá só dão a resposta para os Alunos no inicio ( grande diferencial do Alura, os professores sempre acompanham seu progresso). Cheguei em um ponto que não sei o que pode ser o erro: Era pra ter funcionado o controle de versão.
import{ModelRouter} from '../common/model-router'
import * as restify from 'restify'
import{NotFoundError} from 'restify-errors'
import{User} from './usuarios.model'
class UsersRouter extends ModelRouter<User> {
constructor(){
super(User)
this.on('beforeRender', document=>{
document.password = undefined
})
}
findByEmail = (req, resp, next)=>{ // fazendo busca por email
if(req.query.email){ // se meu email estiver na busca
User.find({email:req.query.email})
.then(this.renderAll(resp, next))
.catch(next)
}else{
next()
}
}
applyRoutes(application: restify.Server){
application.get({path:'/usuarios', version: '2.0.0'},[this.findByEmail, this.findAll])
application.get({path:'/usuarios', version: '1.0.0'},this.findAll) // desse modo por padrão
application.get('/usuarios/:id',[this.validateId, this.findById])
application.post('/usuarios', this.save)
application.put('/usuarios/:id',[this.validateId, this.replace])
application.patch('/usuarios/:id',[this.validateId, this.update])
application.del('/usuarios/:id',[this.validateId, this.delete])
}
}
export const usersRouter = new UsersRouter()
desse jeito. Mostra o seguinte erro no meu nodemon:
(node:7964) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:7964) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
Server failed to start
{ AssertionError [ERR_ASSERTION]: Method 'GET' already declared for route '/usuarios'
at Router._insert (C:\Users\YuriPNP\Desktop\nail-hear\tentativa-2\node_modules\find-my-way\index.js:275:9)
at Router._on (C:\Users\YuriPNP\Desktop\nail-hear\tentativa-2\node_modules\find-my-way\index.js:177:8)
at Router.on (C:\Users\YuriPNP\Desktop\nail-hear\tentativa-2\node_modules\find-my-way\index.js:66:8)
at RouterRegistryRadix.add (C:\Users\YuriPNP\Desktop\nail-hear\tentativa-2\node_modules\restify\lib\routerRegistryRadix.js:42:21)
at Router.mount (C:\Users\YuriPNP\Desktop\nail-hear\tentativa-2\node_modules\restify\lib\router.js:212:20)
at Server.serverMethod [as get] (C:\Users\YuriPNP\Desktop\nail-hear\tentativa-2\node_modules\restify\lib\server.js:1659:33)
at UsersRouter.applyRoutes (C:\Users\YuriPNP\Desktop\nail-hear\tentativa-2\dist\usuario\usuarios.router.js:24:21)
at Promise (C:\Users\YuriPNP\Desktop\nail-hear\tentativa-2\dist\server\server.js:25:28)
at new Promise (<anonymous>)
at Server.initRoutes (C:\Users\YuriPNP\Desktop\nail-hear\tentativa-2\dist\server\server.js:15:16)
at initializeDb.then (C:\Users\YuriPNP\Desktop\nail-hear\tentativa-2\dist\server\server.js:38:52)
at processTicksAndRejections (internal/process/next_tick.js:81:5)
generatedMessage: false,
name: 'AssertionError [ERR_ASSERTION]',
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '==' }
[nodemon] app crashed - waiting for file changes before starting...
Porém, quando eu retiro as duas versões, esse erro some:
import{ModelRouter} from '../common/model-router'
import * as restify from 'restify'
import{NotFoundError} from 'restify-errors'
import{User} from './usuarios.model'
class UsersRouter extends ModelRouter<User> {
constructor(){
super(User)
this.on('beforeRender', document=>{
document.password = undefined
})
}
findByEmail = (req, resp, next)=>{ // fazendo busca por email
if(req.query.email){ // se meu email estiver na busca
User.find({email:req.query.email})
.then(this.renderAll(resp, next))
.catch(next)
}else{
next()
}
}
applyRoutes(application: restify.Server){
application.get({path:'/usuarios', version: '1.0.0'},this.findAll) // desse modo por padrão
application.get('/usuarios/:id',[this.validateId, this.findById])
application.post('/usuarios', this.save)
application.put('/usuarios/:id',[this.validateId, this.replace])
application.patch('/usuarios/:id',[this.validateId, this.update])
application.del('/usuarios/:id',[this.validateId, this.delete])
}
}
export const usersRouter = new UsersRouter()
dessa forma funciona, e não faço ideia do pq. Alguém pode me ajudar( tenho um projeto que tá dando mesmo erro disponibilizado no github)