Guest User

Untitled

a guest
Feb 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. exports.transaction = function transaction (req, res) {
  2. // Imports the Google Cloud client library
  3. const Datastore = require('@google-cloud/datastore');
  4.  
  5. // Your Google Cloud Platform project ID
  6. const projectId = 'moneypenny-dabc6';
  7.  
  8. // Instantiates a client
  9. const datastore = Datastore({
  10. projectId: projectId
  11. });
  12.  
  13. // The kind for the new entity
  14. const kind = 'Transaction';
  15. // The Cloud Datastore key for the new entity
  16. const transactionKey = datastore.key(kind);
  17. // amount currency date recipient reference sender
  18. // Prepares the new entity
  19. const transaction = {
  20. key: transactionKey,
  21. data: {
  22. amount: 0.00,
  23. currency: 'CHF',
  24. date: new Date().toJSON(),
  25. recipient: "Bob",
  26. sender: "Alice",
  27. reference: "Default",
  28. }
  29. };
  30.  
  31. // Saves the entity
  32. datastore.save(transaction)
  33. .then(() => {
  34. console.log(`Saved ${transaction.key}`);
  35.  
  36. const response = `Send ${transaction.data.amount}
  37. ${transaction.data.currency} to ${transaction.data.recipient}`.
  38.  
  39. res.setHeader('Content-Type', 'application/json'); //Requires application/json MIME type
  40. res.send(JSON.stringify({ "speech": response, "displayText": response
  41. //"speech" is the spoken version of the response, "displayText" is the visual version
  42. }));
  43.  
  44. })
  45. .catch((err) => {
  46. console.error('ERROR:', err);
  47. });
  48. };
Add Comment
Please, Sign In to add comment