diff --git a/functions/spanner/index.js b/functions/spanner/index.js index 86e6395241..48c80db124 100644 --- a/functions/spanner/index.js +++ b/functions/spanner/index.js @@ -19,13 +19,8 @@ // Imports the Google Cloud client library const Spanner = require('@google-cloud/spanner'); -// Your Google Cloud Platform project ID -const projectId = 'YOUR_PROJECT_ID'; - // Instantiates a client -const spanner = Spanner({ - projectId: projectId -}); +const spanner = Spanner(); // Your Cloud Spanner instance ID const instanceId = 'my-instance'; @@ -53,9 +48,11 @@ exports.get = (req, res) => { database.run(query) .then((results) => { const rows = results[0]; - var data = []; - rows.forEach((row) => data.push(row.toJSON())); - res.send(data); + res.send(rows.map((row) => row.toJSON())); + }) + .catch((err) => { + res.status(500); + res.send(`Error querying Spanner: ${err}`); }); }; // [END spanner_functions_quickstart] diff --git a/functions/spanner/test/index.test.js b/functions/spanner/test/index.test.js index 78490ae619..bc89b66d0d 100644 --- a/functions/spanner/test/index.test.js +++ b/functions/spanner/test/index.test.js @@ -69,12 +69,11 @@ function getSample () { }; } -test(`get: Gets albums`, async (t) => { +test(`get: Gets albums`, (t) => { const sample = getSample(); const mocks = sample.mocks; - const err = await sample.program.get(mocks.req, mocks.res); - t.falsy(err, null); + const err = sample.program.get(mocks.req, mocks.res); t.true(mocks.spanner.instance.called); t.true(mocks.instance.database.called); t.true(mocks.database.run.calledWith(query));