Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting [Object object] instead of an id #40

Closed
ajkaanbal opened this issue Feb 17, 2015 · 14 comments
Closed

Getting [Object object] instead of an id #40

ajkaanbal opened this issue Feb 17, 2015 · 14 comments

Comments

@ajkaanbal
Copy link

I have these models

Profile.js

module.exports = {

  schema: false, 

  attributes: {

    '*': '', // little hack to get all fields because no schema¬ 
    profiles: {
      collection: 'Subprofile',
      through: 'profileconnection',
      via: 'profile',
      dominant: true
    }

  },

};

Subprofile.js

module.exports = {

  schema: false,
  attributes: {
    '*': '', // little hack to get all fields because no schema¬ 
    profiles: {
      collection: 'Profile',
      through: 'profileconnection',
      via: 'subprofile',
    }
  }
};

profileconnection

module.exports = {

  scheme: false,

  attributes: {

    profileRef: {
      columnName: 'profileRef',
      type: 'string',
      foreignKey: true,
      references: 'profile',
      on: 'id',
      onKey: 'id',
      via: 'subprofileRef'
    },
    subprofileRef: {
      columnName: 'subprofileRef',
      type: 'string',
      foreignKey: true,
      references: 'subprofile',
      on: 'id',
      onKey: 'id',
      via: 'profileRef'
    }

  }
};

And the result is

[
    {
        "alias": "juanito",
        "birthday": "1-01-1980",
        "createdAt": "2015-02-17T16:03:48.000Z",
        "deathday": "12-12-2010",
        "id": "[object Object]",
        "name": "Juan",
        "nationality": "Francesa",
        "professions": "Músico",
        "profiles": [
            {
                "createdAt": "2015-02-17T16:03:48.000Z",
                "id": "#21:0",
                "updatedAt": "2015-02-17T16:03:48.000Z"
            }
        ],
        "profiletype": "Person",
        "updatedAt": "2015-02-17T16:03:48.000Z"
    }
]

I found that is in the function rewriteIds file lib/utils.js line 70 but I don't understand why.

    if(hop.call(model, 'rid')) {
      // case where the @rid is a temporary one
      self.replaceKeyForId(model, 'rid');
      delete model['@rid'];
    }else if(hop.call(model, '@rid')) {
      self.replaceKeyForId(model, '@rid');
    }
@dmarcelino
Copy link
Member

Hi @ajkaanbal, a few things:

  • Which version of OrientDB are you running?
  • Which version of waterline-orientdb? (the code has been changing frequently)
  • Can you please enable logging for waterline-orientdb and share what messages you are getting before your request is made to the database?

@dmarcelino
Copy link
Member

And BTW, it's more likely the error is somewhere in replaceKeyForId(model, key) which is called just before line 70.

@ajkaanbal
Copy link
Author

Hi @dmarcelino

  • OrientDB 1.7.9
  • waterline-orientdb 0.10.31
  • and the logging
  waterline-orientdb:connection:debug DEBUG  OrientDB query: SELECT *, @rid, createdAt, updatedAt, out_profileconnection FROM profile   LIMIT 30 SKIP 0 +4s
  waterline-orientdb:connection:debug DEBUG  params: [object Object]
  Inspected object:
{ params: {} } +0ms
  waterline-orientdb:connection:debug DEBUG  opts.fetchPlan: out_profileconnection:1 out_profileconnection.in:1 +0ms

@ajkaanbal
Copy link
Author

I also thought that it was there but didn't find how to make it work.

I did this (comment the delete and remove the else condition):

if(hop.call(model, 'rid')) {
      // case where the @rid is a temporary one
      self.replaceKeyForId(model, 'rid');
      /// delete model['@rid'];
    }
 if(hop.call(model, '@rid')) {
      self.replaceKeyForId(model, '@rid');
 }

It works but broke the tests

@dmarcelino
Copy link
Member

In that case you would get a @rid object back which is meant to be replaced by a string id, so that explains why it breaks the tests...

Can you try adding a console.log() in line 40, example below, and post the output here:

exports.replaceKeyForId = function replaceKeyForId(model, key) {
  // change id to string only if it's necessary
  var modelId;
  console.log(typeof model[key], model[key]);  // add this line
  if(typeof model[key] === 'object')
    modelId = model[key].toString();
  else
    modelId = model[key];
  if(modelId.indexOf('#-') !== 0){  // ignoring temporary ids
    model.id = modelId;
  }
  delete model[key];
};

@ajkaanbal
Copy link
Author

   waterline-orientdb:connection:debug DEBUG  OrientDB query: SELECT *, @rid, createdAt, updatedAt, out_profileconnection FROM profile   LIMIT 30 SKIP 0 +7s
  waterline-orientdb:connection:debug DEBUG  params: [object Object]
  Inspected object:
{ params: {} } +1ms
  waterline-orientdb:connection:debug DEBUG  opts.fetchPlan: out_profileconnection:1 out_profileconnection.in:1 +0ms
object { '@type': 'document',
  '@class': 'profile',
  profiletype: 'Person',
  name: 'Juan',
  alias: 'juanito',
  birthday: '1-01-1980',
  deathday: '12-12-2010',
  nationality: 'Francesa',
  professions: 'Músico',
  profiles: [ { cluster: 3, position: 53 } ],
  createdAt: Tue Feb 17 2015 10:03:48 GMT-0600 (CST),
  updatedAt: Tue Feb 17 2015 10:03:48 GMT-0600 (CST),
  out_profileconnection:
   { '@type': 'd',
     '@class': 'profileconnection',
     out: [Circular],
     in:
      { '@type': 'd',
        '@class': 'subprofile',
        createdAt: Tue Feb 17 2015 10:03:48 GMT-0600 (CST),
        updatedAt: Tue Feb 17 2015 10:03:48 GMT-0600 (CST),
        in_profileconnection: [Circular],
        '@rid': [Object] },
     '@rid': { cluster: 17, position: 0 } },
  rid: [Circular],
  '@rid': { cluster: 16, position: 0 } }
object { cluster: 17, position: 0 }
object { cluster: 21, position: 0 }
object { '@type': 'd',
  '@class': 'profile',
  profiletype: 'people',
  name: 'Juan',
  createdAt: Tue Feb 17 2015 12:06:25 GMT-0600 (CST),
  updatedAt: Tue Feb 17 2015 12:06:25 GMT-0600 (CST),
  rid: [Circular],
  '@rid': { cluster: 16, position: 1 } }
object { '@type': 'd',
  '@class': 'profile',
  profiletype: 'people',
  name: 'Glennie Marks V',
  alias: 'Arianna_Spinka31',
  birthday: '2-02-2015',
  deathday: '1-02-2014',
  nationality: 'Thailand',
  createdAt: Tue Feb 17 2015 13:18:53 GMT-0600 (CST),
  updatedAt: Tue Feb 17 2015 13:18:53 GMT-0600 (CST),
  rid: [Circular],
  '@rid': { cluster: 16, position: 2 } }
object { '@type': 'd',
  '@class': 'profile',
  profiletype: 'people',
  name: 'Julio Cantú I',
  alias: 'MaríadelCarmen.Araña0',
  birthday: '2-02-2015',
  deathday: '1-02-2015',
  nationality: 'Moldavia',
  createdAt: Tue Feb 17 2015 13:37:31 GMT-0600 (CST),
  updatedAt: Tue Feb 17 2015 13:37:31 GMT-0600 (CST),
  rid: [Circular],
  '@rid': { cluster: 16, position: 3 } }

@dmarcelino
Copy link
Member

I wasn't expecting that 😄 , is that the output of console.log(typeof model[key], model[key]); // add this line?

@dmarcelino
Copy link
Member

@ajkaanbal, I was able to reproduce the issue, it seems the hack '*': '' is what's causing it. I guess you need those because you are running OrientDB schemaless, right? Let me see how difficult it is to fix it.

@ajkaanbal
Copy link
Author

Yes, for that model I need schemaless.
Thank you and let me know if I can be of any further help.

@dmarcelino
Copy link
Member

I've pushed a fix to master, can you please try it and report back.

@ajkaanbal
Copy link
Author

I'm using sails, and something was wrong. I add a console.log in line 40 (like before) and 68.

  waterline-orientdb:connection:debug DEBUG  OrientDB query: SELECT *, @rid, createdAt, updatedAt, out_profileconnection FROM profile   LIMIT 30 SKIP 0 +2s
  waterline-orientdb:connection:debug DEBUG  params: [object Object]
  Inspected object:
{ params: {} } +0ms
  waterline-orientdb:connection:debug DEBUG  opts.fetchPlan: out_profileconnection:1 out_profileconnection.in:1 +0ms
model  { '@type': 'document',
  '@class': 'profile',
  profiletype: 'Person',
  name: 'Juan',
  alias: 'juanito',
  birthday: '1-01-1980',
  deathday: '12-12-2010',
  nationality: 'Francesa',
  professions: 'Músico',
  profiles: [ { cluster: 3, position: 54 } ],
  createdAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
  updatedAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
  out_profileconnection:
   { '@type': 'd',
     '@class': 'profileconnection',
     out: [Circular],
     in:
      { '@type': 'd',
        '@class': 'subprofile',
        profiletype: 'Musician',
        band: 'Nombre de la banda',
        createdAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
        updatedAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
        in_profileconnection: [Circular],
        '@rid': [Object] },
     '@rid': { cluster: 17, position: 0 } },
  rid: [Circular],
  '@rid': { cluster: 16, position: 0 } }
object { cluster: 16, position: 0 }
model  { '@type': 'd',
  '@class': 'profileconnection',
  out:
   { '@type': 'document',
     '@class': 'profile',
     profiletype: 'Person',
     name: 'Juan',
     alias: 'juanito',
     birthday: '1-01-1980',
     deathday: '12-12-2010',
     nationality: 'Francesa',
     professions: 'Músico',
     profiles: [ [Object] ],
     createdAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
     updatedAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
     out_profileconnection: [Circular],
     rid: [Circular],
     id: '#16:0' },
  in:
   { '@type': 'd',
     '@class': 'subprofile',
     profiletype: 'Musician',
     band: 'Nombre de la banda',
     createdAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
     updatedAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
     in_profileconnection: [Circular],
     '@rid': { cluster: 21, position: 0 } },
  '@rid': { cluster: 17, position: 0 } }
object { cluster: 17, position: 0 }
model  { '@type': 'd',
  '@class': 'subprofile',
  profiletype: 'Musician',
  band: 'Nombre de la banda',
  createdAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
  updatedAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
  in_profileconnection:
   { '@type': 'd',
     '@class': 'profileconnection',
     out:
      { '@type': 'document',
        '@class': 'profile',
        profiletype: 'Person',
        name: 'Juan',
        alias: 'juanito',
        birthday: '1-01-1980',
        deathday: '12-12-2010',
        nationality: 'Francesa',
        professions: 'Músico',
        profiles: [Object],
        createdAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
        updatedAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
        out_profileconnection: [Circular],
        rid: [Circular],
        id: '#16:0' },
     in: [Circular],
     id: '#17:0' },
  '@rid': { cluster: 21, position: 0 } }
object { cluster: 21, position: 0 }
model  { cluster: 3, position: 54 }
model  { '@type': 'd',
  '@class': 'subprofile',
  profiletype: 'Musician',
  band: 'Nombre de la banda',
  createdAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
  updatedAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
  in_profileconnection:
   { '@type': 'd',
     '@class': 'profileconnection',
     out:
      { '@type': 'document',
        '@class': 'profile',
        profiletype: 'Person',
        name: 'Juan',
        alias: 'juanito',
        birthday: '1-01-1980',
        deathday: '12-12-2010',
        nationality: 'Francesa',
        professions: 'Músico',
        profiles: [Object],
        createdAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
        updatedAt: Tue Feb 17 2015 17:06:28 GMT-0600 (CST),
        rid: [Circular],
        id: '#16:0' },
     in: [Circular],
     id: '#17:0' },
  id: '#21:0' }
silly: res.ok() :: Sending 200 ("OK") response
Unhandled rejection TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at ServerResponse.res.json (/opt/node_modules/sails/node_modules/express/lib/response.js:189:19)
    at ServerResponse.jsonx (/opt/node_modules/sails/lib/hooks/responses/index.js:217:21)
    at Object.sendOK (/opt/api/responses/ok.js:28:16)
    at ServerResponse.bound [as ok] (/opt/node_modules/lodash/dist/lodash.js:729:21)
    at found (/opt/node_modules/sails/lib/hooks/blueprints/actions/find.js:64:9)
    at bound (/opt/node_modules/lodash/dist/lodash.js:957:21)
    at applyInOriginalCtx (/opt/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:416:80)
    at wrappedCallback (/opt/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:315:18)
    at _normalizeCallback.callback.success (/opt/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:33:31)
    at _switch (/opt/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:48:28)
    at returnResults (/opt/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/basic.js:356:9)
    at /opt/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/basic.js:256:16
    at /opt/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:77:45
    at bound (/opt/node_modules/lodash/dist/lodash.js:957:21)
    at applyInOriginalCtx (/opt/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:416:80)
    at wrappedCallback (/opt/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:315:18)
    at _normalizeCallback.callback.success (/opt/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:33:31)
    at _switch (/opt/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:48:28)
    at /opt/node_modules/waterline-orientdb/lib/associations.js:220:3
    at Db.<anonymous> (/opt/node_modules/waterline-orientdb/lib/connection.js:470:11)
From previous event:
    at module.exports.DbHelper.find (/opt/node_modules/waterline-orientdb/lib/connection.js:467:8)
    at Associations.fetchPlanJoin (/opt/node_modules/waterline-orientdb/lib/associations.js:156:19)
    at Associations.join (/opt/node_modules/waterline-orientdb/lib/associations.js:40:17)
    at Object.module.exports.adapter.join (/opt/node_modules/waterline-orientdb/lib/adapter.js:258:27)
    at module.exports.join (/opt/node_modules/sails/node_modules/waterline/lib/waterline/adapter/dql.js:53:13)
    at _runOperation (/opt/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:400:29)
    at run (/opt/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:69:8)
    at bound.module.exports.find (/opt/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/basic.js:243:16)
    at bound [as find] (/opt/node_modules/lodash/dist/lodash.js:729:21)
    at Deferred.exec (/opt/node_modules/sails/node_modules/waterline/lib/waterline/query/deferred.js:501:16)
    at findRecords (/opt/node_modules/sails/lib/hooks/blueprints/actions/find.js:50:9)
    at routeTargetFnWrapper (/opt/node_modules/sails/lib/router/bind.js:178:5)
    at callbacks (/opt/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/opt/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at pass (/opt/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (/opt/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (/opt/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at /opt/node_modules/sails/lib/router/bind.js:186:7
    at alwaysAllow (/opt/node_modules/sails/lib/hooks/policies/index.js:209:11)
    at routeTargetFnWrapper (/opt/node_modules/sails/lib/router/bind.js:178:5)
    at callbacks (/opt/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/opt/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at pass (/opt/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (/opt/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (/opt/node_modules/sails/node_modules/express/lib/router/index.js:167:11)

@dmarcelino
Copy link
Member

That suggests that the object returned contains circular references and it fails to convert to json. In your code, after getting the record from the database, can you try using removeCircularReferences()?

For example:

Profile.removeCircularReferences(profiles, function(result){
  // result won't have circular references, so it's safe to use.
});

@ajkaanbal
Copy link
Author

ok, adding the removeCircularReference resolves the issue with sails.
And now I get the correct id, many thanks.

    {
        "alias": "juanito",
        "birthday": "1-01-1980",
        "createdAt": "2015-02-18T00:15:43.000Z",
        "deathday": "12-12-2010",
        "id": "#16:1",
        "name": "Juan",
        "nationality": "Francesa",
        "professions": "Músico",
        "profiles": [
            {
                "band": "Nombre de la banda",
                "createdAt": "2015-02-18T00:15:43.000Z",
                "id": "#21:1",
                "profiletype": "Musician",
                "updatedAt": "2015-02-18T00:15:43.000Z"
            }
        ],
        "profiletype": "Person",
        "rid": {
            "alias": "juanito",
            "birthday": "1-01-1980",
            "createdAt": "2015-02-18T00:15:43.000Z",
            "deathday": "12-12-2010",
            "id": "#16:1",
            "name": "Juan",
            "nationality": "Francesa",
            "professions": "Músico",
            "profiles": [
                {
                    "band": "Nombre de la banda",
                    "createdAt": "2015-02-18T00:15:43.000Z",
                    "id": "#21:1",
                    "profiletype": "Musician",
                    "updatedAt": "2015-02-18T00:15:43.000Z"
                }
            ],
            "profiletype": "Person",
            "rid": "#16:1",
            "updatedAt": "2015-02-18T00:15:43.000Z"
        },
        "updatedAt": "2015-02-18T00:15:43.000Z"
    }

@dmarcelino
Copy link
Member

Good to hear that! In the future I'll look into improving schemaless support as I don't think that rid property should be there... It's a low priority for us though, so it may take some time.

dmarcelino added a commit that referenced this issue Feb 23, 2015
…ueries (when collection is schemaless)

Relates to #40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants