简体   繁体   English

如何从 Node.js 中的 function 访问 Cloud Function 的返回值 Map?

[英]How can I access return value of Map from function in Node.js for Cloud Function?

I had read this post How to return values in javascript .我读过这篇文章How to return values in javascript But my question is how do we get the Map value if we derived it from asynchronous function like in this code below:但我的问题是,如果我们从异步 function 派生它,我们如何获得 Map 值,如下面的代码所示:

 async function exampleToken(profile) { let response; const tkAdmin = { admin: true, }; const tkInvestors = { investor: true, }; if (profile == 1) { response = { "token": tkAdmin, "code": 1, }; } else if (profile == 2) { response = { "token": tkInvestors, "code": 2, }; } return Promise.resolve(response); }
I want to use the value from this function using this code: 我想使用此代码使用此 function 中的值:

 const response = await exampleToken(profile); // Is this correct: const code = response.code; const token = response.token; // or const code = response["code"]; const token = response["token"];
Please, help me out. 请帮帮我。 Any tips and trick will be great. 任何提示和技巧都会很棒。 Thank you very much for spending time to read this post. 非常感谢您花时间阅读这篇文章。

Both are correct in Javascript, Javascript都正确,

  • 1- Dot property accessor: object. property. 1- 点属性访问器:object.property。
  • 2- Square brackets property access: object['property'] 2- 方括号属性访问:object['property']
  • 3- Object destructuring: const { property } = object. 3- Object 解构:const { property } = object。

This style is named Object Dot Notation access这种样式命名为 Object Dot Notation access

const code = response.code;
const token = response.token;

and this one is Object Bracket notation access这个是 Object 括号表示法访问

const code = response["code"];
const token = response["token"];

Read more here 在这里阅读更多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有这个 node.js 云 function 但它不起作用? - I have this node.js cloud function but it does not work? 如何从云 function 对 firestore 数据库进行 CRUD 操作? (节点.js) - How to do CRUD operations on a firestore DB from a cloud function? (Node.js) 如何从云端访问 firebase 自定义声明 function - How can I access firebase custom claims from a cloud function 如果 Node.js 中的 Cloud Function 至少有 185 的复杂度,它是否混沌? - Is it chaos if a Cloud Function in Node.js has at least 185 complexity? Firebase 存储 | 从 node.js 云函数上传时文件是“application/octet-stream” - Firebase Storage | File is "application/octet-stream" on upload from node.js cloud function 如何使用无服务器框架通过 AWS API 网关在 Node.js 中编写的 AWS Lambda function 上返回错误? - How do I return errors on an AWS Lambda function written in Node.js through the AWS API Gateway using the Serverless framework? fcmTokens和ID Tokens是一样的吗,如何用Node.js作为云function来验证? - Are fcmTokens and ID Tokens the same and how to verify them with Node.js as a cloud function? 如何使用服务帐户从 python 代码访问 GCP 云 function? - How I can get access to GCP cloud function from python code using service account? 如何使用复杂的 JSON Webhook 中的 Node JS Cloud Function 写入 Cloud Firestore? - How to write to the Cloud Firestore using Node JS Cloud Function from Complex JSON Webhook? 我无法以编程方式使用 node.js 客户端库从 google-cloud-automl 获取 model id - I can't programmatically get the model id from google-cloud-automl with node.js client library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM