0

I am using Sails for a project. I used Chrome POSTMAN to create a model instance. After that, I can not start sails server. The error message is attached below.

I realized that this is due to association I have in the model. How can I use POSTMAN to correctly create User instance?

api/model/User.js:

module.exports = {

connection: 'localDiskDb',

attributes: {

    firstName: {
        type: 'string',
        required: true,
        minLength: 1,
        string: true,
        alphadashed: true
    },

    lastName: {
        type: 'string',
        required: true,
        minLength: 2,
        string: true,
        alphadashed: true
    },

    cars: {
        collection: 'car',
        via: 'owner'
    }    
}

Error message when lifting sails:

 /usr/local/lib/node_modules/sails/node_modules/waterline/lib/
 waterline/core/typecast.js:56

Object.keys(values).forEach(function(key) {

                   ^
TypeError: Object.keys called on non-object
    at Function.keys (native)
    at Cast.run (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/typecast.js:56:10)
    at /usr/local/lib/node_modules/sails/node_modules/sails-disk/lib/database.js:209:46
    at Array.map (native)
    at Database.read (/usr/local/lib/node_modules/sails/node_modules/sails-disk/lib/database.js:208:32)
    at Array.async.auto.checkData [as 0] (/usr/local/lib/node_modules/sails/node_modules/sails-disk/lib/database.js:59:12)
    at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:459:38
    at Array.forEach (native)
    at _each (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:32:24)
    at Object.async.auto (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:430:9)
Travis Webb
  • 14,688
  • 7
  • 55
  • 109
windchime
  • 1,253
  • 16
  • 37
  • You can at least get Sails to lift again by deleting (or moving) you database file (probably `.tmp/localDisk.db`). Then check any models that have associations to make sure the references to other models are correct. – sgress454 Dec 24 '14 at 05:40

1 Answers1

0

Clearly we need to improve the error message in this case.

Your via property is incorrect for the cars association. It should reference the reverse association in the Car model (if you want to be able to populate a car's users in a query) or just removed entirely. So, in api/models/Car.js you could have:

module.exports = {

  attributes: {

    users: {

      collection: "User",
      via: "cars"

    },

    ...other attrs...

  }
}

and then set the via in User.js to users. Also try uppercasing Car in the collection property; I've seen that mysteriously be a problem from time to time.

sgress454
  • 24,870
  • 4
  • 74
  • 92
  • Fixed all those issues. The car model has attribute `user: { model: 'User' }`. I first start sails server, then post to localhost:1337/user/create?firstName=joe&lastName=Small – windchime Dec 24 '14 at 16:15
  • Fixed all those issues. The car model has attribute `user: { model: 'User' }`. I first start sails server with no prior data, then POST to localhost:1337/user/create?firstName=joe&lastName=Small Server runs normal. I use GET localhost:1337/user. The result shows the id of user joe Small is 2(?). After that, I use Ctrl + C to stop the server and tries to lift again. The error message is the same. – windchime Dec 24 '14 at 16:22
  • Did you clear your database first? Might be some bad data in there. – sgress454 Dec 24 '14 at 17:10
  • I used `rm .tmp/localDisk.db` before everything. – windchime Dec 24 '14 at 22:11