簡體   English   中英

如何通過 REST API 使用 mongodb 創建登錄系統?

[英]How to create a login system using mongodb through the REST API?

當我運行此代碼時,我不斷收到錯誤消息,提示 await 僅在異步函數中有效。 我試圖修復它,但我認為我將代碼放在錯誤的位置,因為它不起作用,我不斷收到相同的錯誤消息。

登錄客戶端:

<script>
const logindetails = new Vue({
el: '#logindetails',
data: {

    email: "",
    password: "",

},

methods: {
    login: function (e) {

      const body = { email, password };

        const response = await fetch(
          "http://localhost:3000/authentication/login".then(async response => {
            console.log("success");
          }),
          {
            method: "POST",
            headers: {
              "Content-type": "application/json"
            },

            body: JSON.stringify(body)

          },

        );
    }}})

您需要使您的登錄功能異步。

methods: {
    login: async function (e) {

您需要使您的登錄功能異步 -

methods: {
login: async function() { ...

嗨,馬杜,很高興再次在這里見到你。 好的,所以 Jakub 是正確的,您應該將 async 放在函數后面。 另外,請記住 .then 和 await 做同樣的事情,所以你要么使用一個,要么使用另一個。 在這種情況下,你想要做的是這個

const response = await fetch(
      "http://localhost:3000/authentication/login", 
      {
        method: "POST",
        headers: {
          "Content-type": "application/json"
        },

        body: JSON.stringify(body)

      }

    );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM