我有一个自动化测试,可以在本地运行云函数,以管理员身份登录,并将数据写入 Firestore。之前在非管理员权限下效果很好,现在我们正在将云功能升级为具有管理员权限。我可以在本地很好地运行测试,并且可以在本地提供该功能并使用 Postman 执行它,并且它工作得很好。
然而,当在我们的构建服务器(Travis CI - 付费)上运行时,该函数会抛出错误:14 UNAVAILABLE: Getting metadata from plugin failed with error: Could not refresh access token。
这是登录的第一个测试,因此意外地已经拥有正在刷新的访问令牌。
在本地,我已使用 登录到 firebase firebase login。我已经使用firebase login:ci和为 Travis 导出了登录令牌travis encrypt。
对于为什么我们在 Travis 上而不是在本地看到此错误有什么想法吗?
这是堆栈跟踪:
14 UNAVAILABLE: Getting metadata from plugin failed with error: Could not refresh access token.
at Object.<anonymous>.exports.createStatusError (node_modules/google-gax/node_modules/grpc/src/common.js:87:15)
at Object.onReceiveStatus (node_modules/google-gax/node_modules/grpc/src/client_interceptors.js:1188:28)
at InterceptingListener.Object.<anonymous>.InterceptingListener._callNext (node_modules/google-gax/node_modules/grpc/src/client_interceptors.js:564:42)
at InterceptingListener.Object.<anonymous>.InterceptingListener.onReceiveStatus (node_modules/google-gax/node_modules/grpc/src/client_interceptors.js:614:8)
at callback (node_modules/google-gax/node_modules/grpc/src/client_interceptors.js:841:24)
我正在初始化 Firebase:
const admin = require('firebase-admin');
admin.initializeApp({
'credential': admin.credential.applicationDefault(), …Run Code Online (Sandbox Code Playgroud) 我正在使用 firebase 函数,并且有一个函数可以在用户创建时添加新集合。问题是有时用户在功能完成之前已登录,因此用户已登录但尚未创建新集合(然后我收到错误消息“权限丢失或不足。因为规则找不到该集合”)。我该如何处理?
是否可以仅在所有内容都来自
export const createCollection = functions.auth.user().onCreate(async user => {
try {
const addLanguages = await addFirst();
const addSecondCollection = await addSecond();
async function addFirst() {
const userRef = admin.firestore().doc(`languages/${user.uid}`);
await userRef.set(
{
language: null
},
{ merge: true }
);
return 'done';
}
async function addSecond() {
// ...
}
return await Promise.all([addLanguages, addSecondCollection]);
} catch (error) {
throw new functions.https.HttpsError('unknown', error);
}
});
Run Code Online (Sandbox Code Playgroud)
完成了吗?所以谷歌提供者窗口关闭并且用户仅在此之后登录?(并且不要使用 setTimeouts 等)
firebase firebase-authentication google-cloud-functions google-cloud-firestore
我无法从新闻 api 将数据保存到 firebase。我可以成功获取,但是当我添加保存功能时,它返回此错误:
错误:FIREBASE 致命错误:无法解析 Firebase 网址。请使用https://<YOUR FIREBASE>.firebaseio.com
请看下面我的代码:
exports.getArticles = functions.https.onRequest((req, res) => {
return request(newsURL)
.then(data => save(data))
.then(data => response(res, data, 201))
});
function request(url) {
return new Promise(function (fulfill, reject) {
client.get(url, function (data, response) {
fulfill(data)
})
})
}
function response(res, data, code) {
return Promise.resolve(res.status(code)
.type('application/json')
.send(data))
}
function save(data) {
return admin.database().ref('/feed/news')
.set({ data: data })
.then(() => {
return Promise.resolve(data);
})
}
const admin = require('firebase-admin');
var serviceAccount = require('../serviceaccount.json'); …Run Code Online (Sandbox Code Playgroud) javascript node.js firebase google-cloud-functions google-cloud-firestore
最近,我尝试将我的 firebase 云函数从 javascript 迁移到 typescript,并将这些函数拆分为多个文件。但是,在尝试提供服务和部署时,我不断收到错误消息:
服务时的错误:
函数[functionName]:函数被忽略,因为 Firestore 模拟器不存在或未运行。函数[functionName]:函数被忽略,因为 firebase 模拟器不存在或未运行。
部署时出错:
functions[dataDownload(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /srv/node_modules/fs-extra/lib/mkdirs/make-dir.js:86
} catch {
^
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at …Run Code Online (Sandbox Code Playgroud) 我有一个通过 HTTP 请求触发的 Python 3.7 云函数。
函数执行最多可能需要几秒钟 (2-5)。
在某些情况下,HTTP 请求是使用 Javascript 从网站的前端发送的。
我的问题是,即使浏览器窗口(使用 JS 触发 HTTP 请求)关闭或用户导航到另一个页面,云函数是否会正常完成它的执行。该函数返回“ok”,但在这种情况下,它没有返回它的目的地。
我正在尝试触发一个按计划运行的云函数:
Cloud Scheduler -> Cloud Pub/Sub -> Cloud Functions
我的 Cloud Function 使用来自 Pub/Sub 消息的属性:
let messageTitle = null;
try {
messageTitle = message.attributes.messageTitle;
} catch (e) {
console.error('no title in pub/sub message', e);
}
Run Code Online (Sandbox Code Playgroud)
如何创建具有属性的调度程序?
我尝试在调度程序的“有效负载”字段中输入:
{
"data": "string",
"attributes": {
messageTitle: "TEST 3 title",
messageBody: "TEST 3 body"
},
"messageId": "string",
"publishTime": "string"
}
Run Code Online (Sandbox Code Playgroud)
但它不会在 Pub/Sub 中创建属性。
google-cloud-pubsub google-cloud-functions google-cloud-scheduler
如何将两个字符串值发送到特定的云函数。
这是我如何将一个字符串发送到onCall函数的示例。
index.js onCall() 云函数
exports.updateUserPassword = functions.https.onCall((data, context) => {
const uid = data.text;
});
Run Code Online (Sandbox Code Playgroud)
项目根目录下的authentication.js(调用onCall云函数)
var updateUserPassword = firebase.functions().httpsCallable('updateUserPassword ');
updateUserPassword({text: uid}).then(function(result) {
var userProperties = result.data.userData;
var successfulPasswordChange = userProperties.uid;
})
Run Code Online (Sandbox Code Playgroud)
如何实现发送和接收第二个字符串值的方法。
假设需要在调用函数中进行修改。
({text: uid}) //ex psuedo {text: uid, text: password}
Run Code Online (Sandbox Code Playgroud)
还有如何在 onCall() 函数中检索它
var uid = data.text;
var password = data.text;
Run Code Online (Sandbox Code Playgroud) 在 Flutter 中使用 HttpsCallable 插件时,我正在努力获取要传递到我的 Cloud Function 的参数。
我的 Cloud Functions 中的日志显示没有传递任何参数。
我的云函数index.js
// acts as an endpoint getter for Python Password Generator FastAPI
exports.getPassword = functions.https.onRequest((req, res) => {
const useSymbols = req.query.useSymbols;
const pwLength = req.query.pwdLength;
// BUILD URL STRING WITH PARAMS
const ROOT_URL = `http://34.72.115.208/password?pwd_length=${pwLength}&use_symbols=${useSymbols}`;
const debug = {
pwLenType: typeof pwLength,
pwLen: pwLength,
useSymbolsType: typeof useSymbols,
useSymbols: useSymbols,
};
console.log(req.query);
console.log(debug);
// let password: any; // password to be received
cors(req, res, () => { …Run Code Online (Sandbox Code Playgroud) 现在我正在尝试更新我项目中的图片。我可以更新云火商店中的图片网址。但我也想使用 firebase 云功能从云存储中删除上一张图片。
我想要实现的是,当我上传新图片时,将之前的图片从云存储中删除。
我在“产品”集合中有“样本”字段。当“样本”字段中的图片更新时,我想删除云存储中的原始图片。
但是我在云功能日志控制台中遇到错误。
类型错误:无法读取未定义的属性“forEach”
这是我的云函数代码。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const Firestore = admin.firestore;
const db = Firestore();
exports.onProductUpdate = functions.firestore.document('Product/{productId}').onUpdate(async(snap, context) => {
const deletePost = snap.before.data().sample;
let deletePromises = [];
const bucket = admin.storage().bucket();
deletePost.images.forEach(image => {
deletePromises.push(bucket.file(image).delete())
});
await Promise.all(deletePromises)
})
Run Code Online (Sandbox Code Playgroud)
我想修复这个错误。
javascript node.js google-cloud-storage firebase google-cloud-functions
尝试测试我的 firebase 功能时,我不断收到当前错误。
Found .runtimeconfig.json but the JSON format is invalid.
! TypeError: Cannot read property 'app' of undefined
at Object.<anonymous> (F:\Web Dev Stuff\SEEKIO\seekio\functions\index.js:4:56)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at initializeRuntime (C:\Users\msi\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:680:29)
at process._tickCallback (internal/process/next_tick.js:68:7)
! We were unable to load your functions code. (see above)
Run Code Online (Sandbox Code Playgroud)
我的运行时配置如下所示:
当我删除配置文件并运行firebase functions:config:get > .runtimeconfig.json它时,它只会返回这些变量和相同的文件格式,所以我有点困惑。
我的函数如下所示:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const …Run Code Online (Sandbox Code Playgroud) json firebase algolia google-cloud-functions google-cloud-firestore
firebase ×8
javascript ×3
node.js ×3
algolia ×1
flutter ×1
flutter-web ×1
json ×1
reactjs ×1
travis-ci ×1
typescript ×1