Fix(crud) 404 errors for destroy and destroyAll
This commit is contained in:
		
							
								
								
									
										18
									
								
								src/crud.js
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								src/crud.js
									
									
									
									
									
								
							@@ -244,10 +244,18 @@ export const destroy = ({ server, model, prefix = '/', config }) => {
 | 
			
		||||
    @error
 | 
			
		||||
    async handler(request, reply) {
 | 
			
		||||
      const where = parseWhere(request);
 | 
			
		||||
      if (request.params.id) where[model.primaryKeyField] = request.params.id;
 | 
			
		||||
      const { id } = request.params;
 | 
			
		||||
      if (id) where[model.primaryKeyField] = id;
 | 
			
		||||
 | 
			
		||||
      const list = await model.findAll({ where });
 | 
			
		||||
 | 
			
		||||
      if (!list.length) {
 | 
			
		||||
        return void reply(id
 | 
			
		||||
          ? notFound(`${id} not found.`)
 | 
			
		||||
          : notFound('Nothing found.')
 | 
			
		||||
          );
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      await Promise.all(list.map(instance => instance.destroy()));
 | 
			
		||||
 | 
			
		||||
      const listAsJSON = list.map((item) => item.toJSON());
 | 
			
		||||
@@ -266,9 +274,17 @@ export const destroyAll = ({ server, model, prefix = '/', config }) => {
 | 
			
		||||
    @error
 | 
			
		||||
    async handler(request, reply) {
 | 
			
		||||
      const where = parseWhere(request);
 | 
			
		||||
      const { id } = request.params;
 | 
			
		||||
 | 
			
		||||
      const list = await model.findAll({ where });
 | 
			
		||||
 | 
			
		||||
      if (!list.length) {
 | 
			
		||||
        return void reply(id
 | 
			
		||||
          ? notFound(`${id} not found.`)
 | 
			
		||||
          : notFound('Nothing found.')
 | 
			
		||||
          );
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      await Promise.all(list.map(instance => instance.destroy()));
 | 
			
		||||
 | 
			
		||||
      const listAsJSON = list.map((item) => item.toJSON());
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user