【问题标题】:I have firebase cloud function that working final when request from Postman, but I get error when request from flutter我有firebase云功能,当邮递员发出请求时,该功能最终工作,但当来自颤动的请求时出现错误
【发布时间】:2019-12-09 02:52:58
【问题描述】:

我尝试从高级 REST 客户端发送请求,它运行良好, 但从颤振不起作用。

firebase 函数

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

const quantity = 'quantity';
const colors = 'colors';
const price = 'price';
const db = admin.firestore();

exports.addProductQuantity = functions.https.onRequest((request, response) => {
    const ii = request.get('count');
    const id = request.get('product_id');
    const promise = db.collection(colors).doc(id).get();
    const p2 = promise.then(snapshot => {

        var newVal = snapshot.get(quantity);
        newVal = Number(newVal) + Number(ii); 
        db.collection(colors).doc(id).update({
            quantity: newVal
        });
        return response.status(200).send('OK '+newVal);
    });
    p2.catch(error => {
        console.log(error);
        return response.status(500).send(error.data);
    });
});

颤振代码

  static Future addProductQuantity(Product product) async {
    Map<String, dynamic> map = Map();
    map["product_id"] = "d0001";
    map["count"] = 25;

    final HttpsCallable callable = CloudFunctions.instance
        .getHttpsCallable(functionName: "addProductQuantity");

    try {
      dynamic resp = await callable.call(map);
    } catch (e) {
      print("Cloud Function Error: $e");
    }
  }

firebase 函数日志中的错误

    addProductQuantity
     Error: Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.
        at Object.validateResourcePath (/srv/node_modules/@google-cloud/firestore/build/src/path.js:403:15)
        at CollectionReference.doc (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:1718:20)
        at exports.addProductQuantity.functions.https.onRequest (/srv/index.js:13:43)
        at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:57:9)
        at /worker/worker.js:783:7
        at /worker/worker.js:766:11
        at _combinedTickCallback (internal/process/next_tick.js:132:7)
        at process._tickDomainCallback (internal/process/next_tick.js:219:9)

我在 ARC 中尝试过使用相同的 product_id,效果很好

【问题讨论】:

  • 我有同样的问题,运气好吗?
  • 我更新了我的 Flutter 和 Firebase,它解决了我的问题
  • 有人解决了这个问题吗?

标签: firebase flutter google-cloud-functions


【解决方案1】:

我遇到了同样的问题,在使用邮递员进行测试时,我传递了 raw 数据,然后在云函数中执行 jsonEncode。 当我在 Flutter 中传递 jsonEncoded 数据时,问题就出现了。

所以,我实际上执行了两次 jsonEncode,这造成了问题。 只需从云函数中删除 jsonEncode。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 2021-01-28
    • 2023-02-02
    • 2017-08-31
    • 2020-10-30
    • 2019-06-13
    相关资源
    最近更新 更多