【问题标题】:firestore is rejecting post request from angularjsfirestore 拒绝来自 angularjs 的发布请求
【发布时间】:2021-01-04 17:26:04
【问题描述】:

我正在尝试使用 angularjs 中的 $http.post 方法将新文档添加到集合 Offers

我在控制台中收到此错误:

Possibly unhandled rejection: {
    "data": {
        "error": {
            "code": 400,
            "message": "Invalid JSON payload received. Unexpected token.\nfields%5BcompanyName\n^",
            "status": "INVALID_ARGUMENT"
        }
    },
    "status": 400,
    "config": {
        "method": "POST",
        "transformRequest": [null],
        "transformResponse": [null],
        "jsonpCallbackParam": "callback",
        "url": "https://firestore.googleapis.com/v1beta1/projects/syrian-sales-v2/databases/(default)/documents/Offers",
        "data": "fields%5BcompanyName%5D=qwe&fields%5Bdescription%5D=qwe&fields%5BexpiredDate%5D=qwe&fields%5Btitle%5D=qwe&fields%5BuserId%5D=rsVWKar7UTMwUmcyYJbr6Rif4NN2",
        "headers": {
            "Content-Type": "application/json; charset=UTF-8;",
            "Accept": "application/json, text/plain, /"
        }
    },
    "statusText": "",
    "xhrStatus": "complete"
}

我尝试了很多解决方案,但都没有奏效。

这是控制器代码:

app.controller('insert_controller', function ($scope, $http, $httpParamSerializerJQLike) {

    let userId = "rsVWKar7UTMwUmcyYJbr6Rif4NN2";
    let url = "https://firestore.googleapis.com/v1beta1/projects/syrian-sales-v2/databases/(default)/documents/Offers";

    $scope.insertOffer = function () {
        let companyName = $scope.companyName;
        let title = $scope.title;
        let expiredDate = $scope.expiredDate;
        let description = $scope.description;
        let data = {
            "fields": {
                "companyName": companyName,
                "title": title,
                "expiredDate": expiredDate,
                "description": description,
                "userId": userId,
            }
        };

        $http({
            method: 'POST',
            url: url,
            data: $httpParamSerializerJQLike(data),
            headers: {
                'Content-Type': 'application/json; charset=UTF-8;'
            }
        }).then(function (res) {
            if (res.data){
                $scope.companyName = null;
                $scope.title = null;
                $scope.expiredDate = null;
                $scope.description = null;
                alert('Offer Added Successfully')
            }
        }).catch(function (err) {
            console.log(err);
            throw err
        });
    }
});

任何帮助都会很棒,在此先感谢

编辑:我尝试从邮递员发送一个帖子请求,它适用于我正在使用的相同 url 和相同的 json 数据结构

【问题讨论】:

  • 好像你发送的payload格式不好,我在Json lint上测试过,companyName字段也报错了,这里查看如何创建文档:cloud.google.com/firestore/docs/reference/rest/v1beta1/…
  • @HarifVelarde 谢谢,但我已经解决了问题。
  • 你能分享一下你是如何解决这个问题的吗?这对其他有类似情况的用户很有用
  • @HarifVelarde 是的,当然,这实际上不是一个解决方案,它是一个错字,我使用了错误的数据结构,我应该在每个字段中添加 stringValue 属性,所以真的没什么
  • 我的意思是将其作为答案发布,以便其他人可以将其视为已解决并利用答案

标签: angularjs firebase google-cloud-firestore


【解决方案1】:

问题是我使用了错误的架构。

这是旧架构:

let data = {
    "fields": {
        "companyName": companyName,
        "title": title,
        "expiredDate": expiredDate,
        "description": description,
        "userId": userId,
    }
};

这是正确的架构:

let data = {
        "fields": {
            "companyName": {
                "stringValue": companyName
            },
            "title": {
                "stringValue": title
            },
            "expiredDate": {
                "stringValue": expiredDate
            },
            "description": {
                "stringValue": description
            },
            "userId": {
                "stringValue": userId
            },
        }
    };

【讨论】:

    猜你喜欢
    • 2020-04-15
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    相关资源
    最近更新 更多