【问题标题】:Dart json.encode is not encoding as needed by Firebase FunctionDart json.encode 没有按照 Firebase 函数的需要进行编码
【发布时间】:2018-05-04 19:33:46
【问题描述】:

我已经在这个问题上研究了一段时间,但我似乎无法弄清楚究竟是什么问题。在 Dart(2) 中,json.encode() 似乎没有给我我想要的结果。

我正在传递一个看起来像这样的Map<String, dynamic>

_data = <String, dynamic>{
    'uid': newProfileCreationModel.accountID,
    'displayName': newProfileCreationModel.profileName,
    'greeting': newProfileCreationModel.profileBody ?? '',
    'avatarSrc': newProfileCreationModel.avatarSrc,
    'heroSrc': newProfileCreationModel.heroSrc,
    'accountType': newProfileCreationModel.accountType,
    'cityID': newProfileCreationModel.cityID,
    'cityName': newProfileCreationModel.cityName,
    'country': newProfileCreationModel.countryCode,
    'state': newProfileCreationModel.stateAbv,
  };

并使用它来将其转换为 JSON

final String _jsonData = json.encode(_data);

然后我通过以下方式将其发送到谷歌云功能。

final Uri _uri = Uri.parse(functionUrl);
final String _jsonData = json.encode(_data);
final String _userToken = await AuthenticationService.getUserToken();

HttpClient client = new HttpClient();
HttpClientRequest request = await client.postUrl(_uri);
request.headers.set('Authorization', 'Bearer ' + _userToken);
request.headers.set('Content-Type', 'application/json; charset=utf-8');
print('Encoded: ' + _jsonData);
request.write(_jsonData);

HttpClientResponse response = await request.close();
if(response.statusCode != HttpStatus.OK){ ...

我打印编码字符串的那一行输出到控制台:

05-04 18:52:57.902 26146-26200/com.app.name I/flutter: Encoded: {"uid":'123456789',"displayName":"James","greeting":"My Greetings!","avatarSrc":"http://cdn.free.com/someImage.jpg","heroSrc":"http://cdn.free.com/someImage.jpg","accountType":"per","cityID":1,"cityName":"Eugene","country":"US","state":"OR"}

但是 request.write(_jsonData) 失败并出现以下 firebase 控制台日志错误 Request Body Missing Data

中的响应如下所示

Firebase 控制台日志。

Request body is missing data.  { 
    uid: '123456789',
    displayName: 'James',
    greeting: 'My Greetings!',
    avatarSrc: 'http://cdn.free.com/someImage.jpg',
    heroSrc: 'http://cdn.free.com/someImage.jpg',   accountType: 'per',   
    cityID: 1,
    cityName: 'Eugene',   
    country: 'US',
    state: 'OR' 
}


Invalid request IncomingMessage {
  _readableState: 
   ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: BufferList { head: null, tail: null, length: 0 },
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: true,
     ended: true,
     endEmitted: true,
     reading: false,
     sync: false,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     resumeScheduled: false,
     defaultEncoding: 'utf8',
     ranOut: false,
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },
  readable: false,
  domain: null,
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  socket: 
   Socket {
     connecting: false,
     _hadError: false,
     _handle: 
      TCP {
        bytesRead: 13285,
        _externalStream: {},
        fd: 14,
        reading: true,
        owner: [Circular],
        onread: [Function: onread],
        onconnection: null,
        writeQueueSize: 0,
        _consumed: true },
     _parent: null,
     _host: null,

有趣的部分是数据正在通过,因为它清楚地显示在 firebase 控制台日志中,但是它不会将其识别为主体。

原始数据方法

当我尝试通过request.write() 发送原始 JSON 对象时

request.write({'hello':'universe'});

我在 Firebase 控制台中遇到了一种完全不同的错误。

SyntaxError: Unexpected token h in JSON at position 1
    at Object.parse (native)
    at parse (/var/tmp/worker/node_modules/body-parser/lib/types/json.js:84:17)
    at /var/tmp/worker/node_modules/body-parser/lib/read.js:102:18
    at IncomingMessage.onEnd (/var/tmp/worker/node_modules/raw-body/index.js:149:7)
    at IncomingMessage.g (events.js:292:16)
    at emitNone (events.js:86:13)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

在 firebase 方面,我正在使用一个可调用的函数,这是记录 firebase 控制台日志的地方

export const finalizeProfile = functions.https.onCall((data, context) => { 
    //CODE 
});

是否有人能够发现我可能做错了什么?

【问题讨论】:

  • 对于第一个版本,您可能没有正确设置内容类型的标题,请尝试 request.headers.contentType = new ContentType("application", "json", charset: "utf-8" );在第二个例子中,如果你将一个对象传递给一个请求接收器,它会调用它的 toString。那不会给你json表示。我不知道这些是否能解决您的问题,但它可能会有所帮助
  • 谢谢@JonahWilliams 我也试过了,但无济于事,问题实际上是身体数据的结构方式。我已经提出了解决问题的答案。还没有flutter / dart的云函数SDK,得一步一步来。

标签: json dart google-cloud-functions


【解决方案1】:

解决方案隐藏在显而易见的地方。该问题与缺少的字段有关。对于 Firebase Cloud Functions,错误消息 body is missing data 中提到的内容“正文”字面意思是它需要一个名为 data 的键,其中包含您要传递的数据对象。

谷歌文档对此并不十分清楚,因为他们缺少一个示例https://firebase.google.com/docs/functions/callable-reference#request_body

这是必须将数据发送到functions.https.onCall() 的方式,请注意数据字段与不包含此内容的原始问题相比:

{
   "data":{ 
      "uid":"123456789",
      "displayName":"James",
      "greeting":"My Greeting!",
      "avatarSrc":"http://cdn.free.com/someImage.jpg",
      "heroSrc":"http://cdn.free.com/someImage.jpg",
      "accountType":"per",
      "cityID":1,
      "cityName":"Eugene",
      "country":"OR",
      "state":"AL"
   }
}

现在可以运行的结果代码如下所示:

// Helper function to format any map to be used for Firebase
Map prepMapForFirebaseJSON(Map<String, dynamic> mapData){
    Map<String, Map<String, dynamic>> _fireJson = {
      'data' : mapData
    };
    return _fireJson;
}

// Process HTTP request
final Uri _uri = Uri.parse(functionUrl);
final String _jsonData = json.encode(prepMapForFirebaseJSON(mapData));
final String _userToken = await AuthenticationService.getUserToken();

HttpClient client = new HttpClient();
HttpClientRequest request = await client.postUrl(_uri);
request.headers.set('Authorization', 'Bearer ' + _userToken);
request.headers.set('Content-Type', 'application/json; charset=utf-8');
request.write(_jsonData);
request.close();

【讨论】:

    【解决方案2】:

    我认为缺少关闭请求:

    request.write(_jsonData);
    var response = await request.close();
    

    【讨论】:

    • 我正在修改我的帖子以包含此内容。它已经在我的代码中,但未包含在我原始帖子的代码中。
    猜你喜欢
    • 2022-11-22
    • 2019-05-19
    • 1970-01-01
    • 2017-03-08
    • 2023-03-16
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多