【发布时间】:2020-09-19 13:56:07
【问题描述】:
我正在使用 Flutter 开发我的应用程序,并将 Firebase 作为我的后端,并使用云功能将图像导入应用程序。我已将所有图像 url 存储在云 Firestore 中,因此我编写了一个函数来获取这些图像,但是在从应用程序应用它时,我收到了这个错误。
PlatformException(functionsError, Cloud function failed with exception., {code: INTERNAL, details: null, message: INTERNAL})
index.js
exports.getDownloadUrl = functions.region("asia-south1").https.onCall((data, context) => {
return new Promise((resolve, reject) => {
var colors = {};
var db = admin.firestore();
db.collection('feed')
.document("pets")
.collection("cats")
.get()
.then(snapshot => {
snapshot.forEach(doc => {
var key = doc.id;
var color = doc.data();
color['url'] = key;
colors[key] = color;
});
var colorsStr = JSON.stringify(colors, null, '\t');
console.log('colors callback result : ' + colorsStr);
colors = color;
resolve(colors);
return colors;
}).catch(reason => {
console.log('db.collection("colors").get gets err, reason: ' + reason);
reject(reason);
});
return colors;
});
});
flutter.dart
void cloudFunc() async {
final CloudFunctions cf = CloudFunctions(
app: Firebase.app(),
region: 'asia-south1'
);
final HttpsCallable urlHttpsCallable = cf.getHttpsCallable(
functionName: 'downloadUrl',
);
final HttpsCallableResult urlHttpsCallableResult = await urlHttpsCallable.call();
print("url: " + urlHttpsCallableResult.data.toString());
}
注意:flutter 代码或CloudFunction 的实例化没有任何问题,因为我之前尝试过使用相同的flutter 代码测试helloWorld 函数AND 这段js代码部署完美,没有任何错误。
更新:根据@DougStevenson 的建议,这是来自控制台的日志之一
在 getDownloadUrl 中检测到错误 > {"errorGroup":"CJTI4Nv74rfinQE","errorEvent":{"message":"Unhandled error TypeError: db.collection(...).document is not a function\n at Promise ( /workspace/index.js:39:12)\n 在新的 Promise ()\n 在 exports.getDownloadUrl.functions.region.https.onCall (/workspace/index.js:33:12)\n 在 func (/ workspace/node_modules/firebase-functions/lib/providers/https.js:273:32)\n 在 process._tickCallback (internal/process/next_tick.js:68:7)","eventTime":"2020-09- 19T12:34:11.358Z","serviceContext":{"resourceType":"cloud_function","service":"getDownloadUrl"}},"@type":"type.googleapis.com/google.devtools.clouderrorreporting.v1beta1 .Insight"}
【问题讨论】:
-
在 Functions 控制台日志中查看错误以了解问题所在。这里没有足够的信息来了解发生了什么或如何解决它,因此请使用功能代码生成的特定错误消息更新问题。
-
在一堆废话中,
colors = color;脱颖而出,因为colour不在范围内。即使是这样,分配也没有任何意义。 -
我不太明白,介意详细点吗?
-
'color' 是
.forEach()函子的局部变量。 -
好的,明白了,但这是这段代码中唯一的错误吗?
标签: javascript firebase google-cloud-firestore promise google-cloud-functions