https://discord.cloudflare.com logo
Join Discord
Powered by
# functions
  • z

    zszszsz

    05/06/2023, 4:45 PM
    Use advanced mode, or copy-paste, or compile into /functions
  • g

    Gigabyte

    05/06/2023, 4:54 PM
    I was trying out the D1Database service from Cloudflare and by doing so I created a worker which accomplishes simple tasks involving a database. I then wanted to also try out D1 on pages as that's more where I personally would use it (for full stack apps). But even though I created the database by executing:
    wrangler d1 create pages-test
    and filled it with data by executing:
    wrangler d1 execute pages-test --file=schema.sql
    with the following schema file:
    Copy code
    sql
    CREATE TABLE IF NOT EXISTS users(
      id INTEGER PRIMARY KEY,
      username TEXT NOT NULL UNIQUE,
      email TEXT NOT NULL UNIQUE,
      password TEXT NOT NULL
    );
    
    CREATE TABLE IF NOT EXISTS posts(
      id INTEGER PRIMARY KEY,
      user_id INTEGER NOT NULL,
      title TEXT NOT NULL,
      content TEXT NOT NULL
    );
    
    CREATE TABLE IF NOT EXISTS comments(
      id INTEGER PRIMARY KEY,
      post_id INTEGER NOT NULL,
      user_id INTEGER NOT NULL,
      content TEXT NOT NULL
    );
    which created the database in the dashboard as I expected. I discovered that after I linked the database to my Cloudflare Pages project which I built with my favorite Framework SvelteKit I wasn't able to access the database. While I tried to find the reason for this I saw that my ``env`` exposes a
    __D1_BETA__DB
    variable which isn't what I called my binding (it's called
    DB
    ) although I tryed using that one but it seems like there is nothing on that Object
    {"__D1_BETA__DB": {}}
    . The bindng
    DB
    is entirely not existent on the ``env`` even when it's shown in the dashboard.
  • g

    Gigabyte

    05/06/2023, 4:55 PM
    Can anyone give me any guidance on that or is that a known issue with Cloudflare Page Functions and D1 and for now we can only use D1 in workers?
  • s

    Skye

    05/06/2023, 4:55 PM
    What's your wrangler version?
  • g

    Gigabyte

    05/06/2023, 4:56 PM
    2.19.0
  • s

    Skye

    05/06/2023, 4:57 PM
    Do you have a repo to look at?
  • g

    Gigabyte

    05/06/2023, 4:57 PM
    Not directly but I can push my test code to github just give me a second
  • n

    nora

    05/06/2023, 5:03 PM
    Page Functions doesn't have a wrangler toml file, did you set up the binding in your Pages dashboard?
  • s

    Skye

    05/06/2023, 5:04 PM
    They said that in the steps, yes
  • g

    Gigabyte

    05/06/2023, 5:05 PM
    Yes
  • n

    nora

    05/06/2023, 5:06 PM
    Correct production/preview environment?
  • g

    Gigabyte

    05/06/2023, 5:21 PM
    So here is my code: I also added screenshots showing how it looks on my dashboard.
  • g

    Gigabyte

    05/06/2023, 5:25 PM
    This is the output I get:
  • u

    Unsmart | Tech debt

    05/06/2023, 5:26 PM
    How are you uploading to pages?
  • g

    Gigabyte

    05/06/2023, 5:27 PM
    wrangler pages publish --project-name pages-test ./.svelte-kit/cloudflare
  • s

    Skye

    05/06/2023, 5:28 PM
    Which adapter are you using?
  • g

    Gigabyte

    05/06/2023, 5:29 PM
    Copy code
    js
    import adapter from '@sveltejs/adapter-cloudflare';
  • s

    Skye

    05/06/2023, 5:34 PM
    Did you only add the binding after making a deployment?
  • g

    Gigabyte

    05/06/2023, 5:35 PM
    I did firstly deploy the project to create it then added the binding but I deployed after adding the binding too and it didn't work
  • g

    Gigabyte

    05/06/2023, 5:35 PM
    But now I redeployed and it seems to work
  • g

    Gigabyte

    05/06/2023, 5:35 PM
    the old deployment still returns
  • g

    Gigabyte

    05/06/2023, 5:36 PM
    while the new one now gives
  • s

    Skye

    05/06/2023, 5:36 PM
    It should be available on all future deployments as soon as you save it 🤔
  • s

    Skye

    05/06/2023, 5:36 PM
    Either way, it's there now 🤷‍♀️
  • j

    James

    05/06/2023, 5:37 PM
    Oh the joys of the D1 shim DX 😅
  • g

    Gigabyte

    05/06/2023, 5:39 PM
    yeah, now it seems to work. The DX is not the greatest currently but I'm certain it will become way better when D1 actually is a real product and out of beta. As currently there also only seems to be the way of redeploying it to test new stuff with the db (when using a Framework [I know you can use wrangler dev on a functions directory but currently doesn't work on svelte projects ^^]).
  • z

    zszszsz

    05/06/2023, 5:42 PM
    I believe so
  • f

    four33

    05/08/2023, 6:47 PM
    Having a problem streaming data from a node backend through a cloudflare function to a pages FE - the
    Accept-Encoding: identity
    header is being completely clobbered by CloudFlare and gets re-written to
    gzip, br, deflate
    -- has anyone encountered this problem?
  • f

    four33

    05/08/2023, 7:19 PM
    nvm, was able to resolve by setting
    Cache-Control: no-transform
  • n

    nora

    05/08/2023, 8:00 PM
    Is it possible to get the current deployment # in a request, or maybe set it during build?
1...385386387...392Latest