couchdb Save with cradle api and iriscouch follow api not working in NodeJS function

170 Views Asked by At

I am trying to save a javascript object with cradle in NodeJS but I do not get a response or an error message after the save. Its a local couchdb with no password set. The connection is ok and the database exists. I am also using iriscouch follow https://github.com/iriscouch/follow . So when a change is made in the wages table the save employee method is called. The follow works, as I have debugged it but no save is taking place.

Here is my code.

var employeedb = new(cradle.Connection)().database('employees');

employeedb.exists(function (err, exists) {
    if (err) {
        console.log('error', err);
    } else if (exists) {
        console.log('Database exists');
    } else {
        console.log('database does not exists.');
        db.create();
        /* populate design documents */
    }
});

follow({db:"http://localhost:5984/wages", include_docs:true}, function(error, change) {
    if(!error) {
    var employee ={};
    employee.name = 'Tom';
    saveEmployee(employee);
 }
})

// simple callback after cradle save
function saveHandler(er, doc){
    if (er) return console.log('Error: ', er);
    console.log(doc);
}

function saveEmployee(employee){
    //need to check if result exists for that runner and event
    var returnData = {};
    employeedb.save(employee, saveHandler);
}
1

There are 1 best solutions below

4
On

The code works fine for me, I needed to add

var cradle = require('cradle');

to the top of your code, and change

- employeedb.save(result, saveHandler);
+ employeedb.save(employee, saveHandler);

for it to 'compile', but after that it adds Tom to the database.