1

This works with sails-disk adapter but not with sails-mongo or sails-postgresql.

I have 2 models, Feed and Event, where Feed can belong to many Events.

When I create or save a new Feed record with an association to one or more Events (via "posts" property), the “posts” property is not saved. However, when I create or save a new Event with an association to one Feed (via the "posted" property), the "posted" property is saved properly. The problem: I can't query a Feed and its Events (using Sails's .populate) from the Feed side, only from the Event side.

I can confirm that the data being passed to Feed.create contains a "posts" property and then the response from the callback does not.

sails.log.debug('Before Feed.create', data);

  Before Feed.create { id: 64988,
  username: 'anevening',
  displayName: 'An Evening',
  coverImage: 'http://res.cloudinary.com/dostuff-media/image/upload/v1387144880/metro-bkg_1-30.png',
  profileImage: 'http://res.cloudinary.com/dostuff-media/image/upload/v1387144880/metro-bkg_1-30.png',
  description: undefined,
  links: [],
  source: 
   { domain: 'dola.com',
     id: 64988,
     url: 'http://www.dola.com/artists/an-evening' },
  posts: [ 3055349, 3062549, 2866105, 2866107 ] }

Feed.create(data).exec(sails.log.debug);

  { username: 'anevening',
  displayName: 'An Evening',
  coverImage: 'http://res.cloudinary.com/dostuff-media/image/upload/v1387144880/metro-bkg_1-30.png',
  profileImage: 'http://res.cloudinary.com/dostuff-media/image/upload/v1387144880/metro-bkg_1-30.png',
  description: null,
  links: [],
  source: 
   { domain: 'dola.com',
     id: 64988,
     url: 'http://www.dola.com/artists/an-evening' },
  createdAt: Sat Nov 22 2014 14:53:21 GMT-0800 (PST),
  updatedAt: Sat Nov 22 2014 14:53:21 GMT-0800 (PST),
  id: '64988' }

Here's what my models look like:

Feed.js:

module.exports = {

  attributes: {

    id: {
      type: 'integer',
      unique: true,
      primaryKey: true
    },
    ...
    posts: {
       collection: 'event',
       via: 'posted'
    }
    ...

  }
};

Event.js:

module.exports = {

  attributes: {

    id: {
      type: 'integer',
      unique: true,
      primaryKey: true
    },
    ...
    posted: {
      model: 'feed'
    }
    ...

  }

};

Lastly, here's what my config/models.js and config/connections.js look like:

connections: {

 'default': 'mongo',

    localDiskDb: {
      adapter: 'sails-disk'
    },

    mongo: {
      adapter: 'sails-mongo',
      url: process.env.MONGO_HOST',
      schema: true
    },

    postgres: {
      adapter: 'sails-postgresql',
      url: process.env.POSTGRES_HOST,
      schema: true,
      ssl: true
    }

  },

  models: {
    connection: 'mongo',
    migrate: 'alter'
  }
senornestor
  • 4,075
  • 2
  • 33
  • 33
  • I've created a new post with more detailed statement of the problem: http://stackoverflow.com/questions/27896657/sails-js-many-to-many-associations-throw-error-on-create-and-populate – senornestor Jan 12 '15 at 06:48

1 Answers1

1

The .create method doesn't return the populated data in it's callback. If you query the Feed model and populate the posts association you should see the data.

Feed.find()
.populate('posts')
.exec(console.log);
particlebanana
  • 2,416
  • 21
  • 22
  • Thanks for the quick response, @particlebanana. Unfortunately, `.populate` returns empty values for the association attributes. I have been using Sails console to test `Feed.find().populate('posts').exec(console.log);` with different adapters. With sails-disk, I get the populated values as expected. With sails-mongo, the populated values are empty. With sails-postgresql, I get the following error: `Details: TypeError: Cannot convert null to object undefined ` – senornestor Nov 24 '14 at 19:48
  • I'm noticing that any queries other than `Feed.find()` on my Feed model are returning undefined while queries on my Event model are working as expected. For example, if I take an ID from one of the results from `Feed.find()` and try `Feed.find(12345)`, it returns undefined. I am getting the same results using mongo console. This seems to imply that something is wrong with my Feed model? – senornestor Nov 24 '14 at 20:17