javascript - using new keyword in callback causes "TypeError: object is not a function" -
i have call:
var serve = function(teacher, callback) { re.createfakememberdata(function(err, avgmember) { //omitted }); };
inside createfakememberdata
, if try instantiate mongoose model:
var statistic = new statistic();
i error stack trace:
d:\orkun\workspace\teb\tebesir-repo\scripts\engines\resetengine.js:61 var statistic = new statistic(); ^ typeerror: object not function @ object.createfakememberdata (d:\orkun\workspace\teb\tebesir-repo\scripts\engines\resetengine.js:61:21) @ promise.<anonymous> (d:\orkun\workspace\teb\tebesir-repo\scripts\servlets\graph\servespider-teacher.js:29:20) @ promise.<anonymous> (d:\orkun\workspace\teb\tebesir-repo\node_modules\mongoose\node_modules\mpromise\lib\promise.js:177:8) @ promise.emit (events.js:95:17)
i think 'this'
going out of scope or sth. have tried using bind
..
re.createfakememberdata(function(err, avgmember) { //omitted }).bind(this);
no joy..
edit: statistic
// external libs going used var bunyan = require('bunyan'); var mongoose = require('mongoose'); var _ = require('underscore'); var schema = mongoose.schema; // models going used var subject = require('../../scripts/models/subject'); var cf = require('../../scripts/utilities/commonfunctions'); var resetengine = require('../engines/resetengine'); var metricengine = require('../engines/metricengine'); var constants = require('../../scripts/utilities/constants'); var log = bunyan.createlogger({src: true, name: 'logstatistics'}); var statistic = new schema( { solved_tot: {type: number, default : 0}, // number score_tot: {type: number, default : 0}, // score time_tot: {type: number, default : 0}, // time // statistics subjects available client stats: [{ stat_id: { type: string, required: true}, stat_name: { type: string, required: true}, stat_solved: { type: number, default : 0}, stat_score: { type: number, default : 0}, stat_time: { type: number, default : 0} }] }, { strict: true }); // !param new_score book has been bought book // !param new_time of user makes buy string // !param test_solved book has been bought book // !desc should async performance opt statistic.methods.updatestatisticsfromtest = function(test_solved, new_time, new_score, callback) { (var = 0; < test_solved.subjects.length; i++) { (var k = 0; k < this.stats.length; ++k) { if (test_solved.subjects[i].subject_name == this.stats[k].stat_name) { // set current variables var current_score = this.stats[k].stat_score; var current_time = this.stats[k].stat_time; var current_solved = this.stats[k].stat_solved; // calculate , update 3 indexes this.stats[k].stat_score = (((current_score * current_solved) + new_score) / (current_solved + 1)).tofixed(5); this.stats[k].stat_time = (((current_time * current_solved) + new_time) / (current_solved + 1)).tofixed(5); this.stats[k].stat_solved++; break; } } } // calculate average total time this.time_tot = (((this.time_tot * this.solved_tot) + new_time) / (this.solved_tot + 1)).tofixed(5); // calculate average score this.score_tot = (((this.score_tot * this.solved_tot) + new_score) / (this.solved_tot + 1)).tofixed(5); // increase number of tests solved this.solved_tot++; // save new user stat this.save(function(err) { if (cf.errorcheck(err, constants.db_error, 'save statistic')) return callback(err); return callback(null, this); }); }; // !brief creates empty subject statistics object new user // !param subjectlist (array<subject>) list of subjects included statistics // !callback function called when database query ends statistic.methods.createstatistics = function(subjectlist) { // initialize , add subjects statistics object this.stats = []; (var = 0; < subjectlist.length; i++) { this.stats.push({'stat_id': subjectlist[i].id.tostring(), 'stat_name' : subjectlist[i].name}); } }; // !brief aggregates subject statistics on single topic statistics // !param subjectstatisticsobjectlist ( array <statistic> ) subject statistics aggregated statistic.methods.aggregatestatistic = function(statisticsobjectlist) { resetengine.resetstatistic(this); (var = 0; < statisticsobjectlist.length; i++) { this.solved_tot += statisticsobjectlist[i].solved_tot; this.time_tot += statisticsobjectlist[i].time_tot; this.score_tot += statisticsobjectlist[i].score_tot * statisticsobjectlist[i].solved_tot; (var k = 0; k < statisticsobjectlist[i].stats.length; k++) { this.stats[k].stat_solved += statisticsobjectlist[i].stats[k].stat_solved; this.stats[k].stat_score += statisticsobjectlist[i].stats[k].stat_score / statisticsobjectlist.length; this.stats[k].stat_time += statisticsobjectlist[i].stats[k].stat_time; this.stats[k].subject_watch += statisticsobjectlist[i].stats[k].subject_watch; } } this.score_tot = this.score_tot / statisticsobjectlist.length; resetengine.roundstatistic(this); }; // !brief gets topic statistics in object belong subject // !param thesubject ( subject ) subject going search // !param topiclist ( array<topic> ) topic lists going searched // !return (array<topic>) test found function statistic.methods.gettopicstatblobbysubject = function(subjectobject, topiclist) { var topicstatisticsofsubject = []; (var = 0; < topiclist.length; i++) { if (topiclist[i].subject === subjectobject._id.tostring()) { (var k = 0; k < this.stats.length; k++) { if (this.stats[k].stat_id === topiclist[i]._id.tostring()) { topicstatisticsofsubject.push(this.stats[k]); break; } } } } return topicstatisticsofsubject; }; // !brief find blob belongs given subject or topic name // !param statname ( string ) name of subject or topic blob found // !return subjectblob or topicblob (statistic.stats) statistic.methods.getstatblob = function(statname) { (var = 0; < this.stats.length; i++) { if (this.stats[i].stat_name === statname) { return this.stats[i]; } } }; module.exports = mongoose.model('statistic' , statistic);
Comments
Post a Comment