id is null after save()
erichs opened this issue · 2 comments
erichs commented
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
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 :
- Model validation fails (ex: wrong type, validators method, etc.)
- Redis error (connection/networks)
- Links errors.
erichs commented
Ah! My bad. Thanks!