fixed js function to write to DB