Skip to content

Commit

Permalink
fix err in abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
biggora committed Jan 6, 2016
1 parent da32e12 commit f5faab6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/abstract-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
Expand Down

0 comments on commit f5faab6

Please sign in to comment.