Why catch doesn't fired when i tried to add data row with the same key?
rethinkdb.table('user').insert(user).run(req._rdb).then(result => {
console.log(result);
done(null, user, { message: 'Successfull' });
}).catch(error => done(error));
Output from console.log, not from done(error).
{ deleted: 0,
errors: 1,
first_error: 'Duplicate primary key ...'
inserted: 0,
replaced: 0,
skipped: 0,
unchanged: 0 }
There was an issue about changing this behavior I will see if I can dig it up. There might also have been a flag to change the behavior. For the moment this is working as documented. insert and the rest of the CRUD family of queries return a success document that details how far they progressed.
As @grandquista mentioned, this is expected behavior. Consider that multiple writes can be issued in a single query. If the query executes successfully and perhaps only one of these writes failed it doesn't necessarily make sense to raise an exception. That behavior would be determined by how your application interprets the response.
If you want your query to fail when the write fails, you can do something like this to raise an error on the server (though, I would recommend just raising an exception on the client):
r.table('users').insert({...}).do(result => {
return result('errors').gt(0).branch(
r.error(result('first_error')),
result
)
})
Closing as duplicate of #3440.