Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.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
Javascript js async/await在使用此关键字时抛出错误_Javascript_Node.js_Async Await_Ecmascript 2017 - Fatal编程技术网

Javascript js async/await在使用此关键字时抛出错误

Javascript js async/await在使用此关键字时抛出错误,javascript,node.js,async-await,ecmascript-2017,Javascript,Node.js,Async Await,Ecmascript 2017,这是代码 let x = await this.storeFileName(fileName); 因此,我已经将storeFileName函数声明为async,并且我还返回了一个承诺,在此之前一切正常。但我得到一个错误,它说: SyntaxError:意外标记this,它指向“this”,后跟wait关键字 顺便说一句,我使用的是ES6类,this关键字引用该类的对象 它在没有wait关键字的情况下工作,但是如果我放置wait,它会抛出一个错误 我做错了什么?一切似乎都是正确的。有人能给我点启

这是代码

let x = await this.storeFileName(fileName);
因此,我已经将storeFileName函数声明为async,并且我还返回了一个承诺,在此之前一切正常。但我得到一个错误,它说:

SyntaxError:意外标记this,它指向“this”,后跟wait关键字

顺便说一句,我使用的是ES6类,this关键字引用该类的对象

它在没有wait关键字的情况下工作,但是如果我放置wait,它会抛出一个错误

我做错了什么?一切似乎都是正确的。有人能给我点启示吗

更新:

这是两个功能

   async encodeName(x){
     return new Promise((resolve,reject)=>{
        const cipher = crypto.createCipher('aes192', this.PASSWORD);
        let encrypted = cipher.update(x,'utf8', 'hex');
        encrypted += cipher.final('hex');
        if(encrypted.length>240){
         let x = await this.storeFileName(encrypted);
         resolve(`@Fn ${x}`);
      }
      resolve(encrypted);
     });
   }

   async storeFileName(x){
     return new Promise((resolve,reject)=>{
      let doc = { encName: x };
      filesDb = new db(`${this.mntpnt}/__CORE_rigel.pro/x100.db`);
      filesDb.insert(doc,(err,newdoc)=>{
        err?reject():resolve(newdoc._id);
      });   
     });
   }
顺便说一句,我在node.js上做这个

更新2:

这是错误消息

A JavaScript error occurred in the main process
Uncaught Exception:
/home/teja/Documents/Rigel/components/diskEncryptor.js:32
        let x = await this.storeFileName(encrypted);
                      ^^^^
SyntaxError: Unexpected token this
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/teja/Documents/Rigel/index.js:4:23)

您正在创建一个新函数

返回新的PromiseSolve,拒绝=>{

您也应该将该函数声明为async:

返回新的承诺异步解析,拒绝=>{

新功能将是

async function encodeName(x){
 return new Promise(async (resolve,reject)=>{
    const cipher = crypto.createCipher('aes192', this.PASSWORD);
    let encrypted = cipher.update(x,'utf8', 'hex');
    encrypted += cipher.final('hex');
    if(encrypted.length>240){
     let x = await this.storeFileName(encrypted);
     resolve(`@Fn ${x}`);
    }
    resolve(encrypted);
 });
}

好的,解释如何使函数异步兼容似乎有点棘手

所以这里我已经更新了你的函数来展示我将要做什么

请注意util.promisify,它现在已烘焙到最新版本的节点中

async encodeName(x){
  const cipher = crypto.createCipher('aes192', this.PASSWORD);
  let encrypted = cipher.update(x,'utf8', 'hex');
  encrypted += cipher.final('hex');
  if(encrypted.length>240){
    let x = await this.storeFileName(encrypted);
    return `@Fn ${x}`;
  }
  return encrypted;
}

async storeFileName(x){
  let doc = { encName: x };
  filesDb = new db(`${this.mntpnt}/__CORE_rigel.pro/x100.db`);
  pinsert = util.promisify(filesDb.insert);
  const newdoc = await pinsert(doc);
  return newdoc._id;
}
您不能在未声明为async的新Promise构造函数的executor函数中使用Wait。无论如何

使用


首先,非常感谢并帮助我。我拿起了你们的代码片段并进行了测试,我终于理解了背后的概念。我真的不知道该选谁的答案正确,你们的答案似乎都是正确的。无论如何,谢谢大家,下面是我的想法

   encodeName(x){
     return new Promise(async (resolve,reject)=>{
        const cipher = crypto.createCipher('aes192', this.PASSWORD);
        let encrypted = cipher.update(x,'utf8', 'hex');
        encrypted += cipher.final('hex');
        if(encrypted.length>240){
        let x = await this.storeFileName(encrypted);
        resolve(`@Fn ${x}`);
      }
      resolve(encrypted);
     });
   }

   storeFileName(x,mntpnt){
    return new Promise((resolve,reject)=>{
     let doc = { encName: x };
     filesDb = new db(`${this.mntpnt}/__CORE_rigel.pro/x100.db`);
     filesDb.insert(doc,(err,newdoc)=>{
       err?reject():resolve(newdoc._id);
     });   
    });
  } 

您是否已将let x=wait this.storeFileName;is?函数声明为async?寻求调试帮助的问题此代码为什么不起作用?必须包括所需的行为、特定的问题或错误以及在问题本身中重现此问题所需的最短代码。没有明确问题说明的问题没有用处给其他读者。请参阅:。@Maluen它不是必需的,但是我做的。@Teja它不是必需的,但是我做的它肯定是必需的,如果不将调用它的函数标记为异步,您就不能使用Wait。我们需要看更多的代码。storeFileName的实现是什么样子的。JavaScript中的this关键字是scop的上下文e、 那么您在哪里使用它呢?同样,我们需要看到更多的代码来提供帮助。这可以阻止错误,但首先需要删除承诺。@keith Lol,但它认真地打印了完全相同的错误消息。@Teja,更新您的代码段,并展示您最初所做的。您不想将承诺构造函数与异步混合使用它没有意义。@Pascut不是。@Bergi完全删除新的Promise反模式可以解决问题,您应该这样做,但SyntaxError的根本原因是没有向创建的arrow函数添加async。
   encodeName(x){
     return new Promise(async (resolve,reject)=>{
        const cipher = crypto.createCipher('aes192', this.PASSWORD);
        let encrypted = cipher.update(x,'utf8', 'hex');
        encrypted += cipher.final('hex');
        if(encrypted.length>240){
        let x = await this.storeFileName(encrypted);
        resolve(`@Fn ${x}`);
      }
      resolve(encrypted);
     });
   }

   storeFileName(x,mntpnt){
    return new Promise((resolve,reject)=>{
     let doc = { encName: x };
     filesDb = new db(`${this.mntpnt}/__CORE_rigel.pro/x100.db`);
     filesDb.insert(doc,(err,newdoc)=>{
       err?reject():resolve(newdoc._id);
     });   
    });
  }