Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js wait仅在异步函数error-Azure函数中有效_Node.js_Azure_Async Await_Azure Functions - Fatal编程技术网

Node.js wait仅在异步函数error-Azure函数中有效

Node.js wait仅在异步函数error-Azure函数中有效,node.js,azure,async-await,azure-functions,Node.js,Azure,Async Await,Azure Functions,我试图让我的登录api在azure函数中工作,但它一直说,await仅在异步函数中有效。这是一个异步函数,所以我非常困惑 这条线 const user=wait db.collection('users').findOne({email:userlogging.email}) 正在抛出错误 const jwt = require("jsonwebtoken"); const bcrypt = require("bcrypt"); var MongoClient = require('mong

我试图让我的登录api在azure函数中工作,但它一直说,
await仅在异步函数中有效。这是一个异步函数,所以我非常困惑

这条线
const user=wait db.collection('users').findOne({email:userlogging.email})

正在抛出错误

const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");


var MongoClient = require('mongodb').MongoClient;



module.exports = async function (context, req) {


  MongoClient.connect(process.env.CosmosDBConnectionString, (err, client) => {


    let userLoggingIn = ({ email, password } = req.body);

    console.log("userLoggingIn");
    console.log(userLoggingIn.email);
    let send = response(client, context);

    if (err) send(500, err.message);


    console.log("DBNAME: " + process.env.dbName);
    let user;
    let db = client.db(process.env.dbName);


   const user = await db.collection('users').findOne({ email: userLoggingIn.email })



    console.log("USER");
    console.log(user);


  let userName= user.instagramName;
    if (!user) {

      send(401, { message: "Auth failed" });

    }

    const { username } = user;
    console.log("PASSWORD");
    console.log(context.req.password);
    console.log(user.password);
    const goodPassword = bcrypt.compareSync(context.req.password, user.password);

    if (!goodPassword) {
      return send(401, { message: "Auth failed" });

    }

    const token = jwt.sign(
      {
        email: user.email,
        userId: user._id,
        username: userName
      },
      "secret_this_should_be_longer",
      { expiresIn: "1h" }
    );

    context.res = { status: 200, token: token, expiresIn: 3600, userId: user._id, username: username};


  })
}




function response(client, context) {
  return function (status, body) {
    context.res = {
      status: status,
      body: body
    };

    client.close();
    context.done();
  };
}
在这一行中,接收err和client作为参数的annonymous回调函数是需要异步的函数

MongoClient.connect(process.env.CosmosDBConnectionString, async (err, client) => {
MongoClient.connect(process.env.CosmosDBConnectionString, async (err, client) => {