maritz / nohm

node.js object relations mapper (orm) for redis
http://maritz.github.io/nohm/
MIT License
500 stars 64 forks source link

id is null after save() #121

Closed erichs closed 6 years ago

erichs commented 6 years ago

Hi! I'm using redis 3.2.4, nohm 0.9.8, and the following code:

const nohm = require('nohm').Nohm;
const redis = require('redis').createClient();
nohm.setPrefix('test');

redis.on('ready', () => {
  nohm.setClient(redis);
  doStuff();
})

const myModel = nohm.model('myModel', {
  properties: {
    name: {
      type: 'string',
      validations: [
        'notEmpty'
      ]
    }
  },
  idGenerator: 'increment'
});

function doStuff () {
  const inst = nohm.factory('myModel');
  inst.p('name', 'testname');
  inst.save();
  console.log(`Saved model with id ${inst.id}`);
}

This script outputs:

Saved model with id null

I can see that the keys are incrementing in Redis with multiple saved models, but the id is always null. Am I missing something basic here? I've tried the above with several LTS versions of node, with the same result.

sam2x commented 6 years ago

Save method is async, you should use a callback, try again but changing your code to the following :

inst.save((err, is_link_err, link_err_model_name) => {
   console.log(`Saved model with haid ${inst.id}`);
});

You should also catch your errors in save, since it may occurs for differents reasons :

erichs commented 6 years ago

Ah! My bad. Thanks!