【发布时间】:2017-11-15 22:34:50
【问题描述】:
所以,这里是代码
let x = await this.storeFileName(fileName);
所以,我已将 storeFileName 函数声明为异步,并且我还返回了一个承诺,直到这里一切都很好。但我收到一条错误消息:
SyntaxError: Unexpected token this 指向 'this' 后跟 await 关键字
顺便说一句,我使用的是 ES6 类,而 this 关键字指的是该类的对象。
它可以在没有 await 关键字的情况下工作,但如果我输入 await 它会引发错误。
我做错了什么?一切似乎都是正确的。谁能给我一些启发。
更新:
这是两个函数。
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)
【问题讨论】:
-
你是否将
let x = await this.storeFileName(fileName);所在的函数声明为async? -
“寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题中重现它所需的最短代码本身。没有明确问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable example。"
-
@Maluen 这不是必需的,但我做到了。
-
@Teja
It's not required but yea I did当然是必需的,如果不将调用它的函数标记为async,则不能使用await。 -
我们需要查看更多代码。
storeFileName的实现是什么样的。 JavaScript 中的this关键字是作用域的上下文,那么你在哪里使用它呢?同样,我们需要查看更多代码才能提供帮助。
标签: javascript node.js async-await ecmascript-2017