问题
When I try to associate one collection with another collection using a many-to-many association, I get the following error:
Using sails-mongo:
TypeError: Cannot read property 'where' of undefined
at _afterFetchingJunctorRecords (/Users/grant/Sites/sails/test/node_modules/sails-mongo/node_modules/waterline-cursor/cursor/populateBuffers.js:131:35)
at /Users/grant/Sites/sails/test/node_modules/sails-mongo/lib/collection.js:103:5
at /Users/grant/Sites/sails/test/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/cursor.js:172:16
at commandHandler (/Users/grant/Sites/sails/test/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/cursor.js:720:16)
at /Users/grant/Sites/sails/test/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/db.js:1874:9
at Server.Base._callHandler (/Users/grant/Sites/sails/test/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/connection/base.js:453:41)
at /Users/grant/Sites/sails/test/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:481:18
at MongoReply.parseBody (/Users/grant/Sites/sails/test/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
at null.<anonymous> (/Users/grant/Sites/sails/test/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:439:20)
at emit (events.js:95:17)
Using sails-postgresql:
TypeError: Cannot convert null to object
at hasOwnProperty (native)
at utils.object.hasOwnProperty (/Users/grant/Sites/sails/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/utils.js:28:14)
at /Users/grant/Sites/sails/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/where.js:261:11
at Array.forEach (native)
at WhereBuilder.complex (/Users/grant/Sites/sails/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/where.js:178:36)
at complexWhere (/Users/grant/Sites/sails/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/index.js:245:16)
at find (/Users/grant/Sites/sails/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/index.js:85:23)
at Cursor.populateBuffers [as $populateBuffers] (/Users/grant/Sites/sails/test/node_modules/sails-postgresql/lib/adapter.js:547:31)
at Cursor.run (/Users/grant/Sites/sails/test/node_modules/sails-postgresql/node_modules/waterline-cursor/cursor/cursor.js:45:8)
at runJoins (/Users/grant/Sites/sails/test/node_modules/sails-postgresql/node_modules/waterline-cursor/index.js:51:10)
Test project
I have created a sample Sails app to demonstrate the issue and allow others to test/debug: https://github.com/gnestor/sails-assoications-issue
To reproduce:
Try to create a new record with an association (I've tried using Sails console, Blueprints API, and inside the controllers):
Event.create({source: 'http://domain.com/event/1', posted_by: {source: 'http://domain.com/feed/1'}}).exec(console.log)
Event.create({source: 'http://domain.com/event/3', venue: {source: 'http://domain.com/feed/2'}}).exec(console.log)
Event.create({source: 'http://domain.com/event/2', reposted_by: [{source: 'http://domain.com/feed/2'}, {source: 'http://domain.com/feed/4'}]}).exec(console.log)
Result
You'll notice that the first two work (one-to-many associations) but the third doesn't (many-to-many association). I have tested this a variety of ways (using Sails console, Blueprints API, and inside controllers).
It's worth noting that it does work with sails-disk but not with sails-mongo, sails-postgresql, or sails-mysql, so I am probably doing something wrong...
Link to issue on sails-mongo: https://github.com/balderdashy/sails-mongo/issues/227 Link to issue on sails-postgresql: https://github.com/balderdashy/sails-postgresql/issues/130
来源:https://stackoverflow.com/questions/27896657/sails-js-many-to-many-associations-throw-error-on-create-and-populate