【发布时间】:2019-05-31 22:15:08
【问题描述】:
我正在尝试从 onCall firebase 函数写入 firestore
functions.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
console.log('initialing functions at ' , new Date().toString())
exports.getLinks = functions.runWith({ timeoutSeconds: 540 }).https.onCall( (data,context) => {
console.log('starting to get links ' , new Date().toString())
console.log('data' , data.query, data.limit, data.country, data.uid)
console.log('context auth', context.auth, 'context.auth.uid', context.auth.uid)
// is there anything like admin.setCredentials(context.auth) necessary here?
const queries = admin.firestore().collection('queries');
let uid = data.uid
console.log('uid', uid);
console.log('queries ref', queries)
//probably when trying to write here is not being allowed
queries.doc(uid).set({LinksArrayLength: 'starting'})
.then( r => console.log('writing to firestore 1 result', r))
.catch( err => console.error('writing to firestore 2 error', err))
控制台输出是这样的
2019 年 5 月 31 日星期五 19:01:10 GMT-0300 (GMT-03:00) 开始获取链接
数据在任何地方销售 2 com fwfwqe 上下文认证{ uid: 'f23oij2ioafOIJOeofiwjOIJ', 令牌:{ 问题:'https://securetoken.google.com/was98oinr-fa4c9', aud: 'was234r-f32c9', 授权时间:1559327744, user_id: 'f23oij2ioafOIJOeofiwjOIJ', sub: 'f23oij2ioafOIJOeofiwjOIJ', iat: 1559338208, exp: 1559341808, 电子邮件:'awef3h@gmail.com', email_verified:假, firebase:{身份:[对象],sign_in_provider:'密码'}, uid: 'f23oij2ioafOIJOeofiwjOIJ' } } context.auth.uid f23oij2ioafOIJOeofiwjOIJ uid f1EMxzwjJlTaH3u7RAYsySx0MZV2 查询参考 CollectionReference { _firestore:Firestore { _设置:{ 项目ID:'xxx', firebaseVersion: '7.0.0', libName: 'gccl', libVersion:'1.3.0 fire/7.0.0' },
然后不允许对firestore的写入请求?
writing to firestore 2 error Error: Unexpected error determining execution environment: Invalid response from metadata service: incorrect Metadata-Flavor header.
> at GoogleAuth.<anonymous> (H:\nprojetos\whats_app_sender\firebase_sender\vue_sender\wa_sender\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:164:23)
> at Generator.throw (<anonymous>)
> at rejected (H:\nprojetos\whats_app_sender\firebase_sender\vue_sender\wa_sender\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:20:65)
> at processTicksAndRejections (internal/process/task_queues.js:89:5)
如何确保将 request.auth.uid 发送到 firestore 写入请求?
Firestore 规则
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;//: if request.auth.uid != null;
// even when commented and allowing all requests still giving the error //message
}
}
}
即使完全允许仍继续给出错误。
当某些内容被写入集合('查询')时,我正在尝试写入 firestore 以更新客户端......以便客户端收到函数进度的通知...... 还有更好的方法吗?
在客户端,代码是这样的
客户端
fireApp.firestore().collection('queries').doc(this.getUser.uid).onSnapshot(snap => {
debugger
console.log('snap', snap)
snap.exists ?
snap.docChanges().forEach(async change => {
if (change.type === "modified") {
_vue.updating = true // the function is in progress
【问题讨论】:
标签: node.js firebase google-cloud-firestore google-cloud-functions