Sunday, 19 March 2017

Google datastore returning weird response

Environment details

OS: Ubuntu 16.04
Node.js version: 7.7.3
npm version: 4.1.2
google-cloud-node version: 0.49

I insert the following entity, but it will fail because the entity already exists:

var transaction = datastore.transaction();
                    transaction.run(function(err) {
                        if (err) {
                            // Error handling omitted.
                            return callback('Database Error');
                        }

                    transaction.save([{
                        key: datastore.key(mykind, mykey]),
                        method: 'insert',
                        data: [{
                            name: 'par1',
                            value: val1
                        }, {
                            name: 'par2',
                            value: val2,
                            excludeFromIndexes: true
                        }
                    }]);

                    transaction.commit(function(err) {
                        if (!err) {
                            // Transaction committed successfully.
                            return callback(null, 'Everything is cool!');
                        }
                         console.log(err);
                    });
                });

The error output is very hard to parse. Err returns this:

"{\"error\":\"Error: entity already exists: app: \\\"s~someapp\\\"<br/>path <<br/> Element {<br/> type: \\\"mykind\\\"<br/> name: \\\"mykey\\\"<br/> }<br/>><br/>\"}"

It returns javascript object but it is difficult to parse if you need the error, type, and name fields. It is not in proper format and there are html entities in the response. Am I doing something incorrect and if so how do I fix this? I want to be able to parse this request.



via user2924127

No comments:

Post a Comment