在 JSON 连接的负载字符串中使用变量。

huangapple go评论66阅读模式
英文:

Use variable in JSON concatenated payload string

问题

以下是您要翻译的内容:

"The below payload works if I hardcode the pubblish date, but if I want to use a variable, it breaks, I can't get the correct way of adding a variable in the payload, I can't use template literals as the JS engine is prior to ES6.

var today = new Date();
var publishDate = new Date(new Date().setDate(today.getDate() - 31));
const query = JSON.stringify({query:
'{
all_insight_article(locale: "en-gb"
where: {business_unit: {business_unit: {title: "Capital"}, MATCH: ALL}, audience: {MATCH: ALL, audiences: {title: "Management"}} publish_date_gt:'+publishDate+'
) {items {
system {uid publish_details {time} updated_at}
absolute_url title subtitle main_image
audienceConnection {edges {node {... on Audiences {title system {uid}}}}}
article_typeConnection {edges {node {... on ArticleType {title}}}}
topicsConnection {edges {node {... on Topics {title display_name system {uid}}}}}}total}}"
});

Error:

07/03/2023 14:47:24 js3 []
07/03/2023 14:47:24 js3 INT-150012 The HTTP query returned a 'Unprocessable Entity' type error (422)"

英文:

The below payload works if I hardcode the pubblish date, but if I want to use a variable, it breaks, I can't get the correct way of adding a variable in the payload, I can't use template literals as the JS engine is prior to ES6.

var today = new Date();
var publishDate = new Date(new Date().setDate(today.getDate() - 31));
const query = JSON.stringify({query:
               '{' +
               '   all_insight_article(locale: "en-gb"'+
               '     where: {business_unit: {business_unit: {title: "Capital"}, MATCH: ALL}, audience: {MATCH: ALL, audiences: {title: "Management"}} publish_date_gt:'+publishDate+'}'+
               '   ) {items {'+
               '        system {uid publish_details {time} updated_at}'+
               '        absolute_url title subtitle main_image'+
               '        audienceConnection     {edges {node {... on Audiences   {title system {uid}}}}}'+
               '        article_typeConnection {edges {node {... on ArticleType {title}}}}'+
               '        topicsConnection       {edges {node {... on Topics      {title display_name system {uid}}}}}}total}}'
        });

Error:

07/03/2023 14:47:24	js3	[]
07/03/2023 14:47:24	js3	INT-150012 The HTTP query returned a 'Unprocessable Entity' type error (422)

答案1

得分: 1

以下是代码部分的翻译:

const query = JSON.stringify({query:
               '{' +
               '   all_insight_article(locale: "en-gb"'+
               '     where: {business_unit: {business_unit: {title: "Capital"}, MATCH: ALL}, audience: {MATCH: ALL, audiences: {title: "Management"}} publish_date_gt:"'+publishDate.toISOString()+'"}'+
               '   ) {items {'+
               '        system {uid publish_details {time} updated_at}'+
               '        absolute_url title subtitle main_image'+
               '        audienceConnection     {edges {node {... on Audiences   {title system {uid}}}}}'+
               '        article_typeConnection {edges {node {... on ArticleType {title}}}}'+
               '        topicsConnection       {edges {node {... on Topics      {title display_name system {uid}}}}}}total}}'
        });

这是您提供的代码的翻译部分。

英文:

Just for completeness, my comment as answer:

publishDate is still a Date objects but the query expects a string. So you have to quote it and convert it to an ISO String (Date.toISOString) first:

const query = JSON.stringify({query:
               '{' +
               '   all_insight_article(locale: "en-gb"'+
               '     where: {business_unit: {business_unit: {title: "Capital"}, MATCH: ALL}, audience: {MATCH: ALL, audiences: {title: "Management"}} publish_date_gt:"'+publishDate.toISOString()+'"}'+
               '   ) {items {'+
               '        system {uid publish_details {time} updated_at}'+
               '        absolute_url title subtitle main_image'+
               '        audienceConnection     {edges {node {... on Audiences   {title system {uid}}}}}'+
               '        article_typeConnection {edges {node {... on ArticleType {title}}}}'+
               '        topicsConnection       {edges {node {... on Topics      {title display_name system {uid}}}}}}total}}'
        });

huangapple
  • 本文由 发表于 2023年3月7日 22:52:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定