【问题标题】:NodeJS nedb function not awaitingNodeJS nedb 函数不等待
【发布时间】:2021-06-21 06:37:33
【问题描述】:

函数checkExists 执行时间过长。尝试使用 await async 功能但没有效果。 var exists = await checkExists(data.email); 正在返回 undefined,因为没有等待 checkExists

我有我的 index.js:

const express = require('express');
const app = express();

require('./private/signUpAPI')(app);

app.listen(80, () => console.log('listening on 80'));
app.use(express.static('public'));
app.use(express.json({limit: '1mb'}));

还有我的 signUpAPI.js:

const DataStore = require('nedb');
const express = require('express');

const database = new DataStore('private/database.db');
database.loadDatabase();

module.exports = function api(app){
    app.use(express.json({limit: '1mb'}));
    app.post('/signUpAPI', async (request, response) => {
        console.log("Sign Up Request received!");
        const data = request.body;

        var exists = await checkExists(data.email);

        console.log(exists)
        console.log(data);
        console.log("Added to DB");
        console.log('-------------------------' + '\n');
        database.insert(data);
        const testData = {"status": "success"};
        response.send(testData);
    });    
}

async function checkExists(email){
    var exists = false;

    database.find({"email": email}, async function(err, docs){
        if (docs.length > 0){exists = true;}
        console.log(docs.length);
        return exists;
    });
}

这是运行 index.js 并调用 fetch('/signUpAPI') 时的节点输出:

Sign Up Request received!
undefined
{
  email: 'a',
  username: 'a',
  hashPass: 'da180265625ebeaf62f4ee1813bdc28faeaf79f0b2b329290758a1c095111ae8',
  salt: 'g8VkTBV$+Bh35K9ns7Zt*9^CH#M=VELSzKUX=H3^+5kpFV=bEbVfXFtF*GGYHOa#'
}
Added to DB
-------------------------

37

我目前在数据库中有 37 个条目具有相同的数据,因此 console.log(docs.length) 返回 37。

但这是最后执行的,当它应该出现在顶部时,它出现在控制台的底部。

【问题讨论】:

    标签: javascript node.js database express nedb


    【解决方案1】:

    使用https://www.npmjs.com/package/nedb-promise

    因此您可以使用 await 进行数据库查询,并且可以像这样更改您的代码 -

    async function checkExists(email) {
        const record = await database.findOne({ email });
        console.log(record);
        if (record) return true;
        return false;
    }
    

    【讨论】:

      【解决方案2】:

      您想要等待的函数应该返回一个 Promise 以等待响应。 如果操作结果成功,您要么解决承诺,要么因错误拒绝。

      流程应该是这样的;

      async function func1()
      {
          try
          {
              var tmp = await func2();
              console.log(tmp);
          }
          catch(err)
          {
              console.log(err);
          } 
      }
      
      async funcion func2()
      {
          return new Promise(async function (resolve, reject)
          {
              if(true)
              {
                  resolve("success");
              }
              else
              {
                  reject("error");
              }
          });
      }
      

      【讨论】:

        猜你喜欢
        • 2021-02-10
        • 2021-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-29
        • 2020-09-23
        • 1970-01-01
        相关资源
        最近更新 更多