Netlify lambda函数查询字符串参数在本地环境和生产环境之间以不同的方式传递

Netlify lambda函数查询字符串参数在本地环境和生产环境之间以不同的方式传递,lambda,netlify,query-parameters,netlify-function,Lambda,Netlify,Query Parameters,Netlify Function,描述错误 我创建了几个lambda函数,并将它们部署在Netlify服务上 我通过GET请求传递了几个查询,它们在本地作为数组传递,但一旦部署,它们就作为字符串传递 复制 复制行为的步骤: lambda功能侧: export const handler=constructNetlifyHandler({ get:async(事件、上下文、postgrest)=>{ console.log(event.queryStringParameters); 返回等待getAllTasks( 最后, eve

描述错误

我创建了几个lambda函数,并将它们部署在Netlify服务上

我通过GET请求传递了几个查询,它们在本地作为数组传递,但一旦部署,它们就作为字符串传递

复制 复制行为的步骤:

lambda功能侧:

export const handler=constructNetlifyHandler({
get:async(事件、上下文、postgrest)=>{
console.log(event.queryStringParameters);
返回等待getAllTasks(
最后,
event.queryStringParameters,
event.loggedInUserOrg
);
},
});
邮差方面:

{{host}}/tasks?orgUid=06ea7872-32eb-4e7a-ba45-63e2b9e6c747&statusUid=ba92a0b7-2e80-4cd2-8d37-fa18d5ca22b9&statusUid=78dcdbe1-007a-493a-ad94-50a0ec613d0d&statusUid=1cbc65b8-831d-4cba-a1ad-111a0757e76b
本地环境日志:

{
  orgUid: '06ea7872-32eb-4e7a-ba45-63e2b9e6c747',
  statusUid: [
    'ba92a0b7-2e80-4cd2-8d37-fa18d5ca22b9',
    '78dcdbe1-007a-493a-ad94-50a0ec613d0d',
    '1cbc65b8-831d-4cba-a1ad-111a0757e76b'
  ]
}
netlify生产环境上的日志:

{
  orgUid: '06ea7872-32eb-4e7a-ba45-63e2b9e6c747',
  statusUid: 'ba92a0b7-2e80-4cd2-8d37-fa18d5ca22b9, 78dcdbe1-007a-493a-ad94-50a0ec613d0d, 1cbc65b8-831d-4cba-a1ad-111a0757e76b'
}
预期行为

在这两种环境中,查询参数的传递方式相同。(作为字符串数组或字符串)

屏幕截图

邮差方面:

本地环境日志:

{
  orgUid: '06ea7872-32eb-4e7a-ba45-63e2b9e6c747',
  statusUid: [
    'ba92a0b7-2e80-4cd2-8d37-fa18d5ca22b9',
    '78dcdbe1-007a-493a-ad94-50a0ec613d0d',
    '1cbc65b8-831d-4cba-a1ad-111a0757e76b'
  ]
}

生产环境上的日志:

{
  orgUid: '06ea7872-32eb-4e7a-ba45-63e2b9e6c747',
  statusUid: [
    'ba92a0b7-2e80-4cd2-8d37-fa18d5ca22b9',
    '78dcdbe1-007a-493a-ad94-50a0ec613d0d',
    '1cbc65b8-831d-4cba-a1ad-111a0757e76b'
  ]
}

桌面(请填写以下信息): 运行以下命令
npx envinfo--system--binaries--npmPackages netlify lambda
并附加输出

附加上下文

 System:
    OS: macOS 11.2.3
    CPU: (4) x64 Intel(R) Core(TM) i3-9100 CPU @ 3.60GHz
    Memory: 54.82 MB / 24.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 12.20.1 - ~/.nvm/versions/node/v12.20.1/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.14.10 - ~/.nvm/versions/node/v12.20.1/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  npmPackages:
    netlify-lambda: ^2.0.3 => 2.0.3 

解决方案1:在函数内部进行匹配

由于Netlify重定向机制无法向您提供它匹配的规则的数据,您可以尝试匹配函数中的原始请求,以确定它应该做什么

例如:

_重定向

> /first-thing /.netlify/functions/the-only-function 200! /second-thing
> /.netlify/functions/the-only-function 200! 
only-function.js

exports.handler = async function(event, context) {
    if (event.path === "/first-thing") {
        // do one thing
    } else if (event.path === "/second-thing") {
        // do the other thing
    } else {
        // return 400 status, because the function was invoked raw
    }
}
exports.handler = async function(event, context) {
    if (event.headers["X-Variant"] === "first-thing") {
        // do one thing
    } else if (event.headers["X-Variant"] === "second-thing") {
        // do the other thing
    } else {
        // return 400 status, because the function was invoked raw
    }
}
解决方案2:代理头攻击

重写某些内容时,可以添加自定义代理标题(单击以获取文档)1

使用以下方法解决了相同的示例:

netlify.toml

[[redirects]]
from = "/first-thing"
to = "/.netlify/functions/the-only-function"
status = 200
force = true
headers = {X-Variant = "first-thing"}

[[redirects]]
from = "/second-thing"
to = "/.netlify/functions/the-only-function"
status = 200
force = true
headers = {X-Variant = "second-thing"}
only-function.js

exports.handler = async function(event, context) {
    if (event.path === "/first-thing") {
        // do one thing
    } else if (event.path === "/second-thing") {
        // do the other thing
    } else {
        // return 400 status, because the function was invoked raw
    }
}
exports.handler = async function(event, context) {
    if (event.headers["X-Variant"] === "first-thing") {
        // do one thing
    } else if (event.headers["X-Variant"] === "second-thing") {
        // do the other thing
    } else {
        // return 400 status, because the function was invoked raw
    }
}
希望这能帮助您解决您的具体问题


谢谢,真的很酷!我花了几个星期努力解决这个问题,这个解决方案解决了这个问题。