vladys.bo
vladys.bo

Reputation: 740

RethinkDB on success Update returns empty object

I have wrote an API usin NodeJS + ExpressJS + RethinkDB. But I got a problem with returning data result while doing update or insert.
In the following example function does update. Query works as should, but inside result is empty object {}, but should return object with old/new data and other state data.

Query

router.put( '/users/contact_data/:user_id', ( request, response ) => {
    let user_id = request.params.user_id;
    let updateFields = clean_obj.getCleanObject(request.body);

    r.db(db_name).table( 'User' )
        .get( user_id )
        .do((client)=>{
                return r.db("Planner").table('ContactData')
                    .get(client("contact_data_id"))
                    .update(
                        updateFields,
                        {
                            returnChanges: true
                        }
                    )
            }
        )
        .run( request._rdb )
        .then( cursor => cursor.toArray() )
        .then( result => {
            response.send( result );
        } )
        .catch( error => response.send( error ) );
} );

Expected Result should look like (copied from rethinkdb docs):

{
    deleted: 0,
    errors: 0,
    inserted: 0,
    changes: [
        {
            new_val: {
                id: 1,
                author: "Julius_Caesar",
                title: "Commentarii de Bello Gallico",
                content: "Aleas jacta est",
                views: 207
            },
            old_val: {
                id: 1,
                author: "Julius_Caesar",
                title: "Commentarii de Bello Gallico",
                content: "Aleas jacta est",
                views: 206
            }
        }
    ],
    replaced: 1,
    skipped: 0,
    unchanged: 0
}

I can't understand what am I doing wrong...

Upvotes: 0

Views: 95

Answers (1)

Etienne Laurin
Etienne Laurin

Reputation: 7184

You are treating the response as a cursor:

.then(cursor => cursor.toArray())

But in reality, the response should be a plain object.

Upvotes: 1

Related Questions