从端点读取的数据显示为未定义。

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

Data read from the db using the endpoint appears as undefined

问题

我使用了在 https://stackoverflow.com/a/70799801 找到的第二种方法,并将其应用到我的源代码中。

我目前正在使用的 SvelteKit 版本是 1.0.0-next.73。

然而,当我将数据打印到控制台时,我只能看到 undefined

下面是我的源代码:

/lib/db/mysql.js

import mysql from 'mysql2/promise';

let mysqlconn = null;

export function mysqlconnFn() {
    if (!mysqlconn) {
        mysqlconn = mysql.createConnection({
            host: 'localhost',
            port: 3307,
            user: 'dbuser',
            password: 'dbpassword',
            database: 'dbname'
        });
    }

    return mysqlconn;
}

/routes/index.server.js

import { mysqlconnFn } from '$lib/db/mysql';

export async function get() {
    let mysqlconn = await mysqlconnFn();

    let results = await mysqlconn.query('SELECT * FROM list_user')
        .then(function ([rows, fields]) {
            // console.log(rows);
            return rows;
        })
        .catch(() => {
            console.log("err")
        })

    return { results }
}

/routes/index.svelte

<script>
    export let data
    import { onMount } from "svelte"

    onMount(() => {
        console.log(data)
    });
</script>

如果您能告诉我出了什么问题以及如何解决,我将不胜感激。

英文:

I used the second method found at https://stackoverflow.com/a/70799801 and applied it to my source.

The version of sveltekit I am currently using is 1.0.0-next.73.

However, when I printed the data to the console, I could only see undefined.

从端点读取的数据显示为未定义。

Below is my source code:

/lib/db/mysql.js

import mysql from &#39;mysql2/promise&#39;;

let mysqlconn = null;

export function mysqlconnFn() {
      if (!mysqlconn) {
            mysqlconn = mysql.createConnection({
                  host: &#39;localhost&#39;,
                  port: 3307,
                  user: &#39;dbuser&#39;,
                  password: &#39;dbpassword&#39;,
                  database: &#39;dbname&#39;
            });
      }

      return mysqlconn;
}

/routes/index.server.js

import { mysqlconnFn } from &#39;$lib/db/mysql&#39;;

export async function get() {
      let mysqlconn = await mysqlconnFn();

      let results = await mysqlconn.query(&#39;SELECT * FROM list_user&#39;)
            .then(function ([rows, fields]) {
                  // console.log(rows);
                  return rows;
            })
            .catch(() =&gt; {
                  console.log(&quot;err&quot;)
            })

      return { results }
}

/routes/index.svelte

&lt;script&gt;
      export let data
      import { onMount } from &quot;svelte&quot;

      onMount(() =&gt; {
            console.log(data)
      });
&lt;/script&gt;

I'd be grateful if you could tell me where I went wrong and how to fix it.

答案1

得分: 1

Files in routes have to be called:

  • +page.svelte
  • +page.js/ts
  • +page.server.js/ts
  • +server.js/ts

So not index, also if you want data to load, the function (in +page.server.js/ts) has to be called load.

英文:

Files in routes have to be called:

  • +page.svelte
  • +page.js/ts
  • +page.server.js/ts
  • +server.js/ts

So not index, also if you want data to load, the function (in +page.server.js/ts) has to be called load.

huangapple
  • 本文由 发表于 2023年2月16日 10:38:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467303.html
匿名

发表评论

匿名网友

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

确定