Created
February 6, 2018 03:13
-
-
Save mfurlend/7dd72134ed1796490b9319d3a2759a10 to your computer and use it in GitHub Desktop.
#google #datastore #save #cloud functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.transaction = function transaction (req, res) { | |
// Imports the Google Cloud client library | |
const Datastore = require('@google-cloud/datastore'); | |
// Your Google Cloud Platform project ID | |
const projectId = 'moneypenny-dabc6'; | |
// Instantiates a client | |
const datastore = Datastore({ | |
projectId: projectId | |
}); | |
// The kind for the new entity | |
const kind = 'Transaction'; | |
// The Cloud Datastore key for the new entity | |
const transactionKey = datastore.key(kind); | |
// amount currency date recipient reference sender | |
// Prepares the new entity | |
const transaction = { | |
key: transactionKey, | |
data: { | |
amount: 0.00, | |
currency: 'CHF', | |
date: new Date().toJSON(), | |
recipient: "Bob", | |
sender: "Alice", | |
reference: "Default", | |
} | |
}; | |
// Saves the entity | |
datastore.save(transaction) | |
.then(() => { | |
console.log(`Saved ${transaction.key}`); | |
const response = `Send ${transaction.data.amount} | |
${transaction.data.currency} to ${transaction.data.recipient}`. | |
res.setHeader('Content-Type', 'application/json'); //Requires application/json MIME type | |
res.send(JSON.stringify({ "speech": response, "displayText": response | |
//"speech" is the spoken version of the response, "displayText" is the visual version | |
})); | |
}) | |
.catch((err) => { | |
console.error('ERROR:', err); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment