Sails: Nested blueprint route returning an array of objects instead of one object when including an :id

Created on 7 Feb 2017  路  4Comments  路  Source: balderdashy/sails

Sails version: 0.12.11
Node version: v7.0.0
NPM version: 4.0.3
Operating system: macOS Sierra




GET /model1/:id/model2/:id returns an array of objects (same response as /model1/:id/model2/ (Not expected).
GET /model2/:id returns just one object as expected (Expected).

Model1 has a one to many relationship with Model2.

More specifically, I have 3 sails models with the following one to many relationships:

- User
  - Account
    - Transaction
    - Transaction...
  - Account ...

When I GET /users/:id/accounts/:id, I get an array like so

[
  {
    "user": "d7156354-6587-4614-b92b-7e8091e3311c",
    "id": "e7c41cc3-7b98-482e-8aa3-2cdd42eab3a1"
  }
]

I expected this to return a single Account object, and not an array containing just one Account object.

I get a single Account object as expected when I GET /accounts/:id
I also get a single User object as expected when I GET /users/:id/,

My model files look like so

// User.js
module.exports = {
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {
    id: {
      type: 'string',
      unique: true,
      primaryKey: true,
      defaultsTo: () => uuid.v4(),
    },
    name: {
      type: 'string',
    },
    email: {
      type: 'string',
      unique: true,
    },
    accounts: {
      collection: 'account',
      via: 'user',
    }
  }
};
// Account.js
module.exports = {
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {
    id: {
      type: 'string',
      unique: true,
      primaryKey: true,
      defaultsTo: () => uuid.v4(),
    },
    date: {
      type: 'date',
      defaultsTo: () => new Date(),
    },
    amount: {
      type: 'float',
      defaultsTo: 0.0,
    },
    account: {
      model: 'account',
    },
    type: {
      type: 'string',
      enum: ['credit', 'debit'],
      defaultsTo: 'credit',
    },
    description: {
      type: 'string',
      defaultsTo: () => `Transaction at ${new Date()}`
    }
  }
};
// Transaction.js
module.exports = {
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {
    id: {
      type: 'string',
      unique: true,
      primaryKey: true,
      defaultsTo: () => uuid.v4(),
    },
    date: {
      type: 'date',
      defaultsTo: () => new Date(),
    },
    amount: {
      type: 'float',
      defaultsTo: 0.0,
    },
    account: {
      model: 'account',
    },
    type: {
      type: 'string',
      enum: ['credit', 'debit'],
      defaultsTo: 'credit',
    },
    description: {
      type: 'string',
      defaultsTo: () => `Transaction at ${new Date()}`
    }
  }
};

This looks like a bug, unless I'm doing something wrong.

inconsistency

Most helpful comment

@G2Jose thanks for the detailed explanation! Digging in and will get back to you ASAP-- I agree on the blueprint routes themselves seeming unexpected here vs what's documented.

In the mean time, just to point this out in case it's helpful: Any time you're doing GET /users/1/accounts/2, you can actually just shave off the front and be more concise-- i.e. just do GET /accounts/2 (because 2 is a unique id across _all_ account records).

All 4 comments

Hi @G2Jose! It looks like you missed a step or two when you created your issue. Please edit your comment (use the pencil icon at the top-right corner of the comment box) and fix the following:

  • Verify "I am experiencing a concrete technical issue (aka a bug) with Sails (ideas and feature proposals should follow the guide for proposing features and enhancements (http://bit.ly/sails-feature-guide), which involves making a pull request). If you're not 100% certain whether it's a bug or not, that's okay--you may continue. The worst that can happen is that the issue will be closed and we'll point you in the right direction."
  • Verify "I am not asking a question about how to use Sails or about whether or not Sails has a certain feature (please refer to the documentation(http://sailsjs.com), or post on http://stackoverflow.com, our Google Group (http://bit.ly/sails-google-group) or our live chat (https://gitter.im/balderdashy/sails)."
  • Verify "I have already searched for related issues, and found none open (if you found a related _closed_ issue, please link to it in your post)."
  • Verify "My issue title is concise, on-topic and polite ("jst.js being removed from layout.ejs on lift" is good; "templates dont work" or "why is sails dumb" are not so good)."
  • Verify "I have tried all the following (if relevant) and my issue remains:"
  • Verify "I can provide steps to reproduce this issue that others can follow."

As soon as those items are rectified, post a new comment (e.g. “Ok, fixed!”) below and we'll take a look. Thanks!

*If you feel this message is in error, or you want to debate the merits of my existence (sniffle), please contact [email protected]

Ok, fixed!

@G2Jose thanks for the detailed explanation! Digging in and will get back to you ASAP-- I agree on the blueprint routes themselves seeming unexpected here vs what's documented.

In the mean time, just to point this out in case it's helpful: Any time you're doing GET /users/1/accounts/2, you can actually just shave off the front and be more concise-- i.e. just do GET /accounts/2 (because 2 is a unique id across _all_ account records).

Okay, these routes have been removed in Sails 1.0. As @mikermcneil said, you can get the same functionality by using the more direct URL (e.g. /accounts/2).

It doesn't look like these routes were documented before, but I'll put something in the upgrade guide for 1.0 just to let folks know they're officially gone. Thanks for bringing this to our attention @G2Jose!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Alirezamohammadi picture Alirezamohammadi  路  4Comments

danil-z picture danil-z  路  3Comments

JohnGrisham picture JohnGrisham  路  4Comments

Sytten picture Sytten  路  4Comments

Salakar picture Salakar  路  4Comments