hel*_*rld 1 node.js firebase reactjs google-cloud-functions google-cloud-firestore
你好,我试图用云功能更新firestore中的文档,但是我对云功能如何知道要更新哪个文档感到困惑,这是我的代码
云功能代码
exports.newsletterGreetConfirm = functions.firestore
.document('newsletter/{newsletterId}')
.onUpdate((change, context) => {
const newValue = change.after.data();
console.log(newValue);
return 0;
});
Run Code Online (Sandbox Code Playgroud)
在我的云函数外壳中,我将以下对象传递给了我的newsletterGreetConfirm函数
{email:"test@gmail.com",id:"LgNqtD6Ih7SYPEEz8jJl",subscribedToNewsletterList:true,timestamp:12321312}
Run Code Online (Sandbox Code Playgroud)
我的输出是undefined
我的目标是在用户单击确认电子邮件中的按钮或链接时确认电子邮件地址。仅在构建此功能的第一阶段,感谢您的所有帮助!我也在用react
The idea is to either generate an HTTP request directly to firebase via a URL sent in the email, or to do it through React.
You could send them to the URL example.com/verify/USER_DOC_ID
Assuming you use react-router-dom, and you want to use the web firebase API to avoid creating an HTTP request, you would do something like the following in your React component (this also assumes you are using firestore in React correctly):
import React, {Component} from 'react':
import firestore from './firestore'; //this is your config file
import {withRouter} from 'react-router-dom';
class Verify extends Component {
componentDidMount(){
const {pathname} = this.props.location;
const ID = pathname.split('/')[2];
//DO THE FIREBASE UPDATE HERE
}
render(){...}
export default withRouter(Verify);
Run Code Online (Sandbox Code Playgroud)
Now, you have a few options for where I placed //DO THE FIREBASE UPDATE HERE. You can either create an http request to firebase, or you can use web side firebase. If you're using web side firebase, you would just update the document in firebase (note that you wouldn't need to import firestore in react if you are doing a fetch request.)
the react way involves something like this:
const ref = firebase.firestore().collection('newsletter').doc(ID);
ref.get().then((doc) => {
if(doc.exists()){
ref.update({verified: true});
});
Run Code Online (Sandbox Code Playgroud)
The logic for interacting with firestore is very similar on the backend with the Admin API, however you'll wrap it in an HTTP request in cloud-functions:
exports.verify = functions.https.onRequest((req, res) => {
// admin api call with req params or body
});
Run Code Online (Sandbox Code Playgroud)
With the above code you can either navigate directly here from the email, responding with HTML directly, or you can call it via a fetch request in the react app like fetch("https://example.com/verify/ID").then(...).
如果您想了解我所描述的上述任何一种方法的更多细节,请将其作为一个单独的问题发布,如果您无法在SO上找到它,请告诉我,我将尽力回答。
| 归档时间: |
|
| 查看次数: |
1860 次 |
| 最近记录: |