Search code examples
node.jsmongoose-schema

mongoose static function "realization.fetchTotalPointsPerChild" is not a function


I'd like to get some result from the aggregate function of one model, but I am getting an error like:

realization.fetchTotalPointsPerChild is not a function

Here are my files:

This is my schema: realization.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const actionSchema = new Schema({
    type: {
        type: String,
        required: true
    },
    point: {
        type: Number,
        min: -1001,
        max: 1001,
        required: true
    },
    pointType: {
        type: String,
        required: true
    },
    goal:{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Goals'
    }, 
    penalty: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Penalty'
    }
},{
    timestamps: true
})

const realizationSchema = new Schema({
    child: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Child'
    },
    actions: [actionSchema]
},
{
    timestamps: true
});

realizationSchema.statics.fetchTotalPointsPerChild = function(callback) {
    return this.aggregate([
        { $unwind: "$actions" },
        {
            $group: {
                _id: null,
                totalPointsPerChild: {
                    $sum: "$actions.point"
                }
            }
        }
    ], function(err, result) {
        if (err) callback(err);
        callback(result)
    })
}

module.exports = mongoose.model('Realization', realizationSchema);

This is my router: realizationRouter.js

const express = require('express');
const bodyParser = require('body-parser');
const Realization = require('../models/realization');

const realizationRouter = express.Router();

realizationRouter.use(bodyParser.json());

realizationRouter.route('/:childId/actions/totalPoints')
.get((req, res, next) => {
    Realization.findOne({child: req.params.childId})
    .populate('goals')
    .populate('penalty')
    .then(realization => {
        if(realization) {
            realization.fetchTotalPointsPerChild((err, result) => {
                if(err){
                    next(err);
                }
                res.statusCode = 200;
                res.setHeader('Content-Type', 'application/json');
                res.json(result);
            })
        } else {
            res.statusCode = 200;
            res.setHeader('Content-Type', 'application/json');
            res.json(`There is no action to show from this child ${req.params.childId}`);
        }
    })
    .catch(err => next(err));
});

I've tried changes like substituting statics to methods or instance but for me what makes more sense is statics but I keep getting the same error. Please someone knows what I am missing here?


Solution

  • if(realization) {
       realization.fetchTotalPointsPerChild((err, result) => {
       if(err){
       next(err);
       }
       res.statusCode = 200;
       res.setHeader('Content-Type', 'application/json');
       res.json(result);
    })
    

    I think you are confused about realization variable. The realization variable is the result of a query. It is not your model that is returned, so there is no function. The static can be used directly on your model so Realization.fetchTotalPointsPerChild()...