node.js - 将 JSON 消息发送到 Azure 队列存储

标签 node.js azure azure-functions

我正在使用azure-storage Node 模块。 我想在我的队列上发送 JSON 并通过 azure 函数将其放入我的队列中。

我在队列中发送消息。我将消息字符串化以放入队列中。

// This is my service who send message to queue via node lib azure-storage
       const queueMsg = {
              userId,
              token: tokenNotif
            };
            queueSvc.createMessage(Config.REGISTRATION_FCM_PUSH_NOTIFICATION_QUEUE, JSON.stringify(queueMsg), (err) => {
              if (!error) {
                this._logger.info(`User ${userId} has register this push notification token`);
                resolve(true);

              } else {
                reject(false);
              }
            });

在队列函数中我有一个错误,因为该函数认为不是字符串并将消息文本推送到 xx-queue-poison {“userId”:“a6c8a103-dacc-4b15-bffd-60693105f131”,“token”:“xxxx”}

我不知道为什么队列中的引号会被替换为 ASCII 代码?

我还测试了其他东西! 从我的服务中,我调用一个 Http Azure 函数,该函数调用队列存储,它的工作方式如下:s ..

HttpTrigger函数调用队列 context.bindings.notificationQueue = [{ userId: name, token }];

并对接收到的数据进行排队 context.log(`收到 userId ${queueItem.userId}::${queueItem.token}`);

为什么通过使用 HttpTrigger 函数到 QueueTrigger 函数它可以工作,但是当我使用 lib“azure-storage”时却不起作用?

谢谢

最佳答案

I don't know why the quote is replaced by ASCII code on queue ?

基本上,SDK 正在转换字符串消息以使其成为 XML 安全的。如果您查看 SDK 中的代码,默认情况下它使用 TextXmlQueueMessageEncoder 作为消息编码器。 encode 函数将 " 替换为 " 以使其成为 XML 安全的。

来自SDK代码(部分代码片段):

QueueService.prototype.createMessage = function (queue, messageText, optionsOrCallback, callback) {
  var userOptions;
  azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; });

  validate.validateArgs('createMessage', function (v) {
    v.string(queue, 'queue');
    v.queueNameIsValid(queue);
    v.callback(callback);
  });

  var xmlMessageDescriptor = QueueMessageResult.serialize(messageText, this.messageEncoder);
<小时/>
function TextXmlQueueMessageEncoder(){
}
util.inherits(TextXmlQueueMessageEncoder, QueueMessageEncoder);

/**
 * Encode utf-8 string by escaping the xml markup characters.
 * @this TextXmlQueueMessageEncoder
 * 
 * @param   {string}    [input]               The target to be encoded.
 * 
 * @return {string}
 */
TextXmlQueueMessageEncoder.prototype.encode = function(input){
  return input.replace(/&/gm, '&amp;')
    .replace(/</gm, '&lt;')
    .replace(/>/gm, '&gt;')
    .replace(/"/gm, '&quot;')
    .replace(/'/gm, '&apos;');
};

一种可能的解决方案是按照您的建议将字符串转换为 Base64 编码的字符串。但是,如果您使用 SDK 检索消息,则您不应在消息正文中看到这些 ",因为 SDK 负责对消息进行解码。

关于node.js - 将 JSON 消息发送到 Azure 队列存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56768711/

相关文章:

eclipse - nodeclipse 一次只允许调试 1 个 .js 文件

Javascript:在字符串中转义 %d

azure - 当我使用“描述”属性时,机器人无法理解该值

c# - Azure Function 上的 HttpRequest 不适用于 https

c# - 在 using 指令 ​​ "using Accord.Video.FFMPEG"中,命名空间中不存在 FFMPEG

node.js - user.save() 不会在 mongodb 中保存(不执行任何操作)

azure - 部署连续的Azure WebJob和WebApp会阻止App

c# - 如何在运行时添加 Azure Functions

azure - 如何使用azure逻辑应用程序在azure数据湖中上传文件

JavaScript 服务器端?