dresende/node-orm2

Association.save is not a function

SrTreze opened this issue · 1 comments

Hi!

Apparently, my server crashes when I try to use a setter in a hasMany relationship.

I have this:

db.models.target_contents.extendsTo('multimedia', {
	title              : { type: 'text'        , required : true },
	picDataURL : { type: 'text'        },
	duration       : { type: 'integer'   , required : true },
	type	             : { type: 'boolean' , required : true },  // False -> audio, True -> video
	uploadDate  : { type: 'date'       , time        : true , required: true },
	lyrics            : { type: 'text'        }
})

db.define('gender', {
	gender : { type: 'text' , unique: true , required: true }
})

db.models.target_contents_multimedia.hasMany('gender', db.models.genders, {}, {autoFetch: true, mergeTable: "songGender", reverse: 'songs'})
db.models.target_contents_multimedia.hasOne('owner', db.models.target_account, { required: true }, {reverse: 'content'})

And I have a route that gets me every song from a owner, and for each, I want to set a gender. For that, I do this:

router.post('/songs/:artistID/:offset', function(req, res){
	req.models.target_contents_multimedia.find({owner_target_tId: req.params.artistID, type: false}).limit(10).offset(req.params.offset).each(function(song){
		song.setGender(9, function(err){
			if (err) console.log(err)
                        else res.send("ok")
		})
	})
})

Problem is, when he gets to the setter, it throws the following error:

TypeError: Association.save is not a function
    at saveAssociation (/Users/joaomiguelrodrigues/Desktop/FCUL/ProjetoFinal/node_modules/orm/lib/Associations/Many.js:378:19)
    at Object.exports.wait (/Users/joaomiguelrodrigues/Desktop/FCUL/ProjetoFinal/node_modules/orm/lib/Hook.js:26:10)
    at saveNextAssociation (/Users/joaomiguelrodrigues/Desktop/FCUL/ProjetoFinal/node_modules/orm/lib/Associations/Many.js:423:12)
    at run (/Users/joaomiguelrodrigues/Desktop/FCUL/ProjetoFinal/node_modules/orm/lib/Associations/Many.js:427:12)
    at Object.value (/Users/joaomiguelrodrigues/Desktop/FCUL/ProjetoFinal/node_modules/orm/lib/Associations/Many.js:452:5)
    at Query._callback (/Users/joaomiguelrodrigues/Desktop/FCUL/ProjetoFinal/node_modules/orm/lib/Associations/Many.js:294:39)
    at Query.Sequence.end (/Users/joaomiguelrodrigues/Desktop/FCUL/ProjetoFinal/node_modules/mysql/lib/protocol/sequences/Sequence.js:86:24)
    at Query._handleFinalResultPacket (/Users/joaomiguelrodrigues/Desktop/FCUL/ProjetoFinal/node_modules/mysql/lib/protocol/sequences/Query.js:137:8)
    at Query.OkPacket (/Users/joaomiguelrodrigues/Desktop/FCUL/ProjetoFinal/node_modules/mysql/lib/protocol/sequences/Query.js:72:10)
    at Protocol._parsePacket (/Users/joaomiguelrodrigues/Desktop/FCUL/ProjetoFinal/node_modules/mysql/lib/protocol/Protocol.js:280:23)

I tried doing this because the getter was returning an empty array, even thou I entered an insert query into database.

Does anyone know why this happens? Or how to solve this? Thanks!

Versions:
"node": "^3.10.9",
"mysql": "^2.13.0",
"orm": "^3.2.3",

dxg commented

How are you calling .save ?