Nishchit
Nishchit

Reputation: 19094

Sails-mongo many to many association not working

I am using sails 0.10.5 , and i have two models as below for many to many relation. but i am getting error while lifting app.

model 1) Category.js

attributes: {
        name: {
            type: 'string',
            required: true
        },
        features: {
            type: 'array'
        },
        desc: {
            type: 'string'
        },
        sub_cats: {
            collection: 'SubCategory',
            via: 'cats',
            dominant: true
        }
    }

model 2) SubCategory.js

attributes: {

        cats: {
            collection: 'Category',
            via: 'sub_cats'
        },
        name: {
            type: 'string',
            required: true
        },
        features: {
            type: 'array'
        },
        desc: {
            type: 'string'
        }
    }

Now while lifting sails app, this error always trigger

"C:\Program Files (x86)\JetBrains\PhpStorm 8.0.2\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" app.js

c:\wamp\www\holymantra\hm\node_modules\sails\node_modules\waterline\node_modules\waterline-schema\lib\waterline-schema\utils.js:47
  return hop.call(obj, prop);
             ^
TypeError: Cannot convert null to object
    at hasOwnProperty (native)
    at exports.object.hasOwnProperty (c:\wamp\www\holymantra\hm\node_modules\sails\node_modules\waterline\node_modules\waterline-schema\lib\waterline-schema\utils.js:47:14)
    at JoinTables.parseAttribute (c:\wamp\www\holymantra\hm\node_modules\sails\node_modules\waterline\node_modules\waterline-schema\lib\waterline-schema\joinTables.js:148:26)
    at c:\wamp\www\holymantra\hm\node_modules\sails\node_modules\waterline\node_modules\waterline-schema\lib\waterline-schema\joinTables.js:83:22
    at Array.forEach (native)

So i am confused (my code is wrong or there is some problem in sails-mongo many-many relation)

--ND

Upvotes: 0

Views: 474

Answers (2)

smileham
smileham

Reputation: 1460

Hopefully you've solved your problem by now! For others like me who ran into this error, balderdashy/waterline-schema just released a more helpful error message at https://github.com/balderdashy/waterline-schema/issues/17 which should help you figure out your particular problem. Because as of today (2015-04-28) npm install does not get you this most recent version, you can manually fix it by doing the following:

  1. Navigate to node_modules/sails/node_modules/waterline/node_modules/waterline-schema/lib/waterline-schema (note: this may be in your global sails directory)
  2. Replace joinTables.js with https://github.com/balderdashy/waterline-schema/blob/8f5a44d076ca36edea409bf41ac2cdbf760c2c59/lib/waterline-schema/joinTables.js

I know this doesn't exactly answer your question as to what went wrong, but hopefully it will give others with that error the tools to solve it more quickly.

Upvotes: 1

roign
roign

Reputation: 462

It is just a typo in SubCategory.js: It should be attributes instead of attribute.

Upvotes: 1

Related Questions