0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 16:22:44 +00:00

Add fields to Mongoose query

This commit is contained in:
G.Ambatte
2022-04-28 11:09:32 +12:00
parent 3fb4c5bdd9
commit cffe08b785
2 changed files with 7 additions and 5 deletions

View File

@@ -194,7 +194,9 @@ app.get('/download/:id', asyncHandler(async (req, res)=>{
app.get('/user/:username', async (req, res, next)=>{
const ownAccount = req.account && (req.account.username == req.params.username);
let brews = await HomebrewModel.getByUser(req.params.username, ownAccount)
const fields = 'title pageCount description authors views shareId editId createdAt updatedAt lastViewed';
let brews = await HomebrewModel.getByUser(req.params.username, ownAccount, fields)
.catch((err)=>{
console.log(err);
});

View File

@@ -37,9 +37,9 @@ HomebrewSchema.statics.increaseView = async function(query) {
return brew;
};
HomebrewSchema.statics.get = function(query){
HomebrewSchema.statics.get = function(query, fields=null){
return new Promise((resolve, reject)=>{
Homebrew.find(query, (err, brews)=>{
Homebrew.find(query, fields, null, (err, brews)=>{
if(err || !brews.length) return reject('Can not find brew');
if(!_.isNil(brews[0].textBin)) { // Uncompress zipped text field
unzipped = zlib.inflateRawSync(brews[0].textBin);
@@ -52,13 +52,13 @@ HomebrewSchema.statics.get = function(query){
});
};
HomebrewSchema.statics.getByUser = function(username, allowAccess=false){
HomebrewSchema.statics.getByUser = function(username, allowAccess=false, fields=null){
return new Promise((resolve, reject)=>{
const query = { authors: username, published: true };
if(allowAccess){
delete query.published;
}
Homebrew.find(query).lean().exec((err, brews)=>{ //lean() converts results to JSObjects
Homebrew.find(query, fields).lean().exec((err, brews)=>{ //lean() converts results to JSObjects
if(err) return reject('Can not find brew');
return resolve(brews);
});