Expand my Community achievements bar.

SOLVED

Delete operation in aio files

Avatar

Level 3
 
const { Core } = require('@adobe/aio-sdk')
const { errorResponse, stringParameters, checkMissingRequestInputs } = require('../utils')
const filesLib = require('@adobe/aio-lib-files')
async function main(params) {
 
const logger = Core.Logger('main', { level: params.LOG_LEVEL || 'info' })

try {
// 'info' is the default level if not set
logger.info('Calling the main action to delete Brief file')

// log parameters, only if params.LOG_LEVEL === 'debug'
logger.debug(stringParameters(params))

//Check for missing request input parameters and headers
const requiredParams = ['briefids']
const requiredHeaders = ['Authorization']
const errorMessage = checkMissingRequestInputs(params, requiredParams, requiredHeaders)
if (errorMessage) {
// return and log client errors
return errorResponse(400, errorMessage, logger)
}
const files = await filesLib.init()
const existingfile = await files.list('/brief/')
if (!existingfile.length) {
errorMessage('400', `can not perform delete operation`, logger)
}
else {
await files.delete(`brief/${params.briefids}.JSON`)
const body = []
for (let { name } of existingfile) {
if(name===(`brief/${params.briefids}.json`))
{
 
}
else
{
const buffer=await files.read(`${name}`)
body.push(JSON.parse(buffer.toString()))
}
}
return {
status: 200,
message: params.briefids,
body
}

}
 
}
catch (error) {
// log any server errors
logger.error(error)
// return with 500
return errorResponse(500, 'server error', logger)
}
}
exports.main = main
 
when I am performing a delete operation via---await files.delete(`brief/${params.briefids}.JSON`)
but I am getting all the listed files along with the deleted ones,
 
 
 
1 Accepted Solution

Avatar

Correct answer by
Employee

Hi Shubham,

As @EstebanBustamante suggested, you will need to call the list method again.

In your code change 

const existingfile = await files.list('/brief/')

to 

let existingfile = await files.list('/brief/')

 

And after calling the delete method make following change

await files.delete(`brief/${params.briefids}.JSON`)
existingfile = await files.list('/brief/')

 

View solution in original post

2 Replies

Avatar

Community Advisor

Hi

 

I think you should call the files.list method again after the delete operation to get the updated list of files.



Esteban Bustamante

Avatar

Correct answer by
Employee

Hi Shubham,

As @EstebanBustamante suggested, you will need to call the list method again.

In your code change 

const existingfile = await files.list('/brief/')

to 

let existingfile = await files.list('/brief/')

 

And after calling the delete method make following change

await files.delete(`brief/${params.briefids}.JSON`)
existingfile = await files.list('/brief/')