I am working on a package of files for download, using MongoDB version 4.4.8 and mongoose version 5.13.5, and I am running into an issue. Whenever a file is removed from the package, it does not update. Here is code I am trying to run:
app.post('/remove-package-file', (req, res, next) => {
let { package, file } = req.body
if (!package || !file) return res.status(500).json({ error: true, message: 'Please specify file and package to remove' })
Package.findOne({ _id: package }).then(pack => {
if (!pack) return res.status(500).json({ error: true, message: 'Unable to locate package' })
if (!pack.files.includes(file)) return res.status(500).json({ error: true, message: 'File is not in package' })
File.findOne({ _id: file }).then(file => {
if (!file) return res.status(500).json({ error: true, message: 'Unable to locate file' })
pack.files.pull(file._id)
pack.size = pack.size-file.stats.size
pack.markModified('size')
pack.markModified('files')
pack.save().then(result => {
console.log(result)
Package.findOne({_id: result._id}).then(result2 => {
console.log(result2)
res.status(200).json({ error: false, package: result2 })
}).catch(e => {
saveError(e)
return res.status(500).json({ error: true, message: 'DB error' })
})
}).catch(e => {
saveError(e)
return res.status(500).json({ error: true, message: 'DB error' })
})
}).catch(e => {
saveError(e)
return res.status(500).json({ error: true, message: 'DB error' })
})
}).catch(e => {
saveError(e)
return res.status(500).json({ error: true, message: 'DB error' })
})
})
Outputs:
{
titles: [ '6127d98a5b5df7462cb166d5' ],
files: [ '6127da305b5df7462cb166db' ],
accessLog: [],
successfulDownloads: [],
timesDownloaded: 0,
recipients: [ '6126da9ac081145ab46039e6' ],
_id: 6128363fc03f9d23f86258ac,
size: -281171,
expires: 2021-08-28T00:47:59.654Z,
name: 't2',
createdBy: '6126da9ac081145ab46039e6',
link: 'http://localhost:4232/download-package?_id=6128363fc03f9d23f86258ac',
date: 2021-08-27T00:47:59.658Z,
__v: 7
}
{
titles: [ '6127d98a5b5df7462cb166d5' ],
files: [ '6127da9c5b5df7462cb166f9', '6127da305b5df7462cb166db' ],
accessLog: [],
successfulDownloads: [],
timesDownloaded: 0,
recipients: [ '6126da9ac081145ab46039e6' ],
_id: 6128363fc03f9d23f86258ac,
size: -281171,
expires: 2021-08-28T00:47:59.654Z,
name: 't2',
createdBy: '6126da9ac081145ab46039e6',
link: 'http://localhost:4232/download-package?_id=6128363fc03f9d23f86258ac',
date: 2021-08-27T00:47:59.658Z,
__v: 7
}
It is showing that the file is updated correctly, but when queried afterward it still has the same values in the files array but the size is changed and it has a new version number. Please let me know if you have any idea why this is happening.