问题标签 [netlify-function]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
32 浏览

javascript - netlify 函数不能与 axios 标头一起使用吗?

好的,所以我正在尝试使用 netlify 函数进行 api 调用,如果我尝试调用另一个不需要标头的 api,它可以正常工作,但是当我包含标头时,承诺返回为“未定义”

到目前为止,这是我的代码,任何帮助都将不胜感激

0 投票
1 回答
51 浏览

javascript - 从 Netlify 函数返回 Promise

我正在开发一个托管在 Netlify 上的无服务器功能,以便为用户订阅 mailchimp 电子邮件列表。

我收到以下晦涩的错误:

lambda response was undefined. check your function code again

这是我的功能:

我认为问题可能是因为调用 submit() 后没有返回任何内容,但我不确定如何最好地返回它。我仍然无法完全理解承诺。

我真的希望有人能指出我正确的方向。

非常感谢大卫

0 投票
0 回答
44 浏览

netlify-function - Nuxt3 + Netlify 函数 + 重写的问题

我的重写不适用于 netlify/production,但在本地运行它们时我没有问题netlify dev

netlify.toml看起来像这样:

npm run build命令只是调用nuxi build.

现在在本地主机上我可以打电话/api/something,它可以正常工作。部署到 netlify 时,相同的调用失败。当我使用/.netlify/functions/something.

现在奇怪的是:当我publish = "dist"netlify.toml文件中删除时,我可以调用/api/something生产,但是 nuxt 代码不起作用,就像所有页面一样。

我首先设置了功能,我的桌面客户端已经在使用它,当我刚刚使用时它会中断,/.netlify/functions/something因为它们已经在使用/api/something.

有人有类似的构建可以帮助我吗?

0 投票
0 回答
22 浏览

reactjs - Mailchimp API 返回 404

我有一个带有表单的 react-app,我正在尝试使用 Netlify-functions 来处理对 Mailchimp-API 的 API 调用,以跟踪注册的电子邮件地址。单击表单上的提交按钮时,我在控制台中收到以下消息。

我已经尝试获取新的 API 密钥,仔细检查了所有依赖项,但我似乎无法让它工作。

这是我从表单到 Netlify 函数的 POST 请求:

这是 Netlify 函数中对 Mailchimp-API 的 POST 请求:

0 投票
0 回答
15 浏览

javascript - 如果图像太大,Netlify 函数会截断请求正文

我正在尝试在 datocms 上上传图像,图像首先以 base64 编码,然后发送到 netlify 函数,在该函数中写入临时文件并上传到 datocms。上传器处理大小为 100kb 的图像,但无法处理大小为 400kb 的图像。我认为请求会被截断,因为在这种情况下,当我在 netlify 端解析 json 时,这会返回错误“位置 0 处的意外令牌”。有没有办法确保请求正文不会被截断?

0 投票
1 回答
38 浏览

javascript - .netlify/functions 的 Vercel 版本是什么?

我正在尝试使用 Vercel 而不是 Netlify部署此存储库: https ://github.com/DataStax-Examples/astra-tik-tok。

我将原版 React 重构为 Next.js,但在Home.js文件中我不明白如何将其迁移到 Vercel 的等价物:

有任何想法吗?

0 投票
0 回答
92 浏览

netlify - 如何在 Netlify 函数中获取原始请求正文?

我正在为现有应用程序开发一个后端,该应用程序将 Gzipped 数据发送到 Netlify Functions 上的端点。但是,我无法将正文作为缓冲区而不是字符串。

这意味着 Zlib 无法解压缩正文,因为无效字节已被 Unicode 替换字符替换,它们变成EF BF BD. 这是我的代码:

这可以通过使用此命令重现。它发送“Hello World!” 用 Gzip 压缩:

但是,由于替换,我的代码不会产生预期的输出:

有什么方法可以访问请求的原始正文,最好是作为缓冲区?

0 投票
2 回答
79 浏览

serverless - How to run a Netlify function in Svelte

Problem description

I would like to run a Netlify function in a Svelte app. However, during development, the requests for the function cannot be found (404). I assume this has to do with the port on which Netlify is serving the functions.

The issue only arises in development. When the app is deployed on Netlify, the function call works just fine. And it also works fine without Svelte.

I have used the basic Svelte template. My Netflify function is called "number" and just returns "42".

number.js:

When I run netlify dev, this starts Netlify's development server with Svelte. It logs:

But http://localhost:8888/.netlify/functions/number does give a 404. Only http://localhost:52041/.netlify/functions/number works here. Can someone explain the first log then, isn't this just incorrect?

The problem now is that the functions port changes every time I call netlify dev. We can fix this by changing the Netlify configuration file:

netlify.toml:

In the end, I would like to add a button to my Svelte app which makes a fetch request to that function.

App.svelte:

This works now in development, but not in production: Here the url should be /.netlify/functions/number.

So my question is:

  • How can we determine the correct URL which works in each case (dev/prod)?
  • Is there a more elegant way to call a Netlify function in a Svelte app?
  • In particular, is it possible to use the URL /.netlify/functions/number somehow in both cases (dev/prod)?

Related questions on the internet:

0 投票
0 回答
23 浏览

reactjs - 如何配置 Create React App 以使用 Netlify Lambda 函数

我正在尝试将 netlify lambda 函数与创建反应应用程序一起使用,它正在破坏我的网站。

该 repo 是通过运行npx create-react-app my-app-name创建的,并且是标准的 create react app 样板。

文件结构:

根目录/package.json

根目录/netlify.toml:

src/setupProxy.js:

src/lambda/dictionary.js:

现在,当我尝试运行npm run start时,应用程序将无法加载。

浏览器显示错误:

当您在浏览器中运行npm run lambda并访问 urlhttp://localhost:9000/.netlify/functions/dictionary时,浏览器会按预期显示“hello, world”。

此外,我无法使用 netlify cli,因为当我尝试安装它时,即使使用 sudo,我也会收到权限错误/访问被拒绝。因此,试图让这种非全局安装的方式工作。