From f5faab6e0aa9195b995143be7f59cb669c44cfa0 Mon Sep 17 00:00:00 2001 From: Alexey Gordeyev Date: Wed, 6 Jan 2016 11:03:14 +0200 Subject: [PATCH] fix err in abstract class --- lib/abstract-class.js | 9 +++++++-- package.json | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/abstract-class.js b/lib/abstract-class.js index ea4d73d..d8ee743 100644 --- a/lib/abstract-class.js +++ b/lib/abstract-class.js @@ -1206,6 +1206,7 @@ AbstractClass.hasMany = function hasMany(anotherClass, params) { * This optional parameter default value is false, so the related object will be loaded from cache if available. */ AbstractClass.belongsTo = function (anotherClass, params) { + var modelName = this.modelName; var methodName = params.as; var fk = params.foreignKey; @@ -1217,7 +1218,7 @@ AbstractClass.belongsTo = function (anotherClass, params) { multiple: false }; - this.schema.defineForeignKey(this.modelName, fk); + this.schema.defineForeignKey(modelName, fk); this.prototype['__finders__'] = this.prototype['__finders__'] || {}; this.prototype['__finders__'][methodName] = function (id, cb) { @@ -1226,10 +1227,14 @@ AbstractClass.belongsTo = function (anotherClass, params) { return; } anotherClass.findById(id, function (err, inst) { + if(!inst) { + return cb(new Error(modelName + ' belongsTo ' + anotherClass.modelName + ' via foreign key ' + fk + ' error')); + } var sid = typeof inst.id === 'object' ? inst.id.toString() : inst.id; var fid = typeof this[fk] === 'object' ? this[fk].toString() : this[fk]; - if (err) + if (err) { return cb(err); + } if (!inst) return cb(null, null); if (sid === fid) { diff --git a/package.json b/package.json index 6d9d62e..b8f4265 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "caminte", "description": "ORM for every database: redis, mysql, neo4j, mongodb, rethinkdb, postgres, sqlite, tingodb", - "version": "0.2.4", + "version": "0.2.5", "author": { "name": "Aleksej Gordejev", "email": "aleksej@gordejev.lv",