TSLint:表达式的类型为“void”。把它作为一个声明单独放在一行
TSLint: Expression has type `void`. Put it on its own line as a statement
当我 运行 firebase deploy
CLI 应用 运行 时,TS 编译器和 TSLint 抛出注释行中显示的错误:
express.get("/shopify/callback", async (req: Request, res: Response): Promise<Response|void> => {
const {shop, code, state} = req.query;
// parse the cookie string into an array. if state cookie is "", err
const stateCookie = cookie.parse(req.headers.cookie as string || "").state;
if (!stateCookie) return res.status(400).send("No state cookie");
if (state !== stateCookie) return res.status(403).send('Cookie failed verification');
const {hmac, ...params} = req.query;
const queryParams = queryString.stringify(params);
const hash = generateEncryptedHash(queryParams); // ERROR: /home/owner/PhpstormProjects/shopify/projectName/functions/src/index.ts:157:15 - Expression has type `void`. Put it on its own line as a statement.
if (hash !== hmac) return res.status(400).send("HMAC validation failed");
我不明白它想要什么改变,有谁知道如何处理这个错误?这是该行上 运行 的辅助函数:
const generateEncryptedHash = (params: unknown) => {
if (typeof SHOPIFY_API_SECRET === "string") {
crypto.createHmac("sha256", SHOPIFY_API_SECRET).update(params as DataView).digest('hex');
} else {
throw Error("during generateEncryptedHash() SHOPIFY_API_SECRET was not a string")
}
};
完整的终端错误输出如下:
$ firebase deploy --only functions
=== Deploying to 'projectName'...
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions@ lint /home/owner/PhpstormProjects/shopify/projectName/functions
> tslint --project tsconfig.json
ERROR: /home/owner/PhpstormProjects/shopify/projectName/functions/src/index.ts:157:15 - Expression has type `void`. Put it on its own line as a statement.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ lint: `tslint --project tsconfig.json`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/owner/.npm/_logs/2019-12-01T16_46_05_460Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code2
您没有 return 来自 generateEncryptedHash
的任何内容,因此是 void return 类型。要比较字符串,您必须 return 来自该函数的内容。像那样:
const generateEncryptedHash = (params: unknown): string => {
if (typeof SHOPIFY_API_SECRET === "string") {
return crypto.createHmac("sha256", SHOPIFY_API_SECRET).update(params as DataView).digest('hex');
} else {
throw new Error("during generateEncryptedHash() SHOPIFY_API_SECRET was not a string")
}
};
我不确定 DataView
是什么,但您只能使用字符串/缓冲区更新 digest
。所以你必须使用 JSON.stringify
如果输入是一个对象并且这就是你想要的哈希值,例如.update(JSON.stringify(params))
UPD:您应该为参数设置类型 params
当我 运行 firebase deploy
CLI 应用 运行 时,TS 编译器和 TSLint 抛出注释行中显示的错误:
express.get("/shopify/callback", async (req: Request, res: Response): Promise<Response|void> => {
const {shop, code, state} = req.query;
// parse the cookie string into an array. if state cookie is "", err
const stateCookie = cookie.parse(req.headers.cookie as string || "").state;
if (!stateCookie) return res.status(400).send("No state cookie");
if (state !== stateCookie) return res.status(403).send('Cookie failed verification');
const {hmac, ...params} = req.query;
const queryParams = queryString.stringify(params);
const hash = generateEncryptedHash(queryParams); // ERROR: /home/owner/PhpstormProjects/shopify/projectName/functions/src/index.ts:157:15 - Expression has type `void`. Put it on its own line as a statement.
if (hash !== hmac) return res.status(400).send("HMAC validation failed");
我不明白它想要什么改变,有谁知道如何处理这个错误?这是该行上 运行 的辅助函数:
const generateEncryptedHash = (params: unknown) => {
if (typeof SHOPIFY_API_SECRET === "string") {
crypto.createHmac("sha256", SHOPIFY_API_SECRET).update(params as DataView).digest('hex');
} else {
throw Error("during generateEncryptedHash() SHOPIFY_API_SECRET was not a string")
}
};
完整的终端错误输出如下:
$ firebase deploy --only functions
=== Deploying to 'projectName'...
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions@ lint /home/owner/PhpstormProjects/shopify/projectName/functions
> tslint --project tsconfig.json
ERROR: /home/owner/PhpstormProjects/shopify/projectName/functions/src/index.ts:157:15 - Expression has type `void`. Put it on its own line as a statement.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ lint: `tslint --project tsconfig.json`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/owner/.npm/_logs/2019-12-01T16_46_05_460Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code2
您没有 return 来自 generateEncryptedHash
的任何内容,因此是 void return 类型。要比较字符串,您必须 return 来自该函数的内容。像那样:
const generateEncryptedHash = (params: unknown): string => {
if (typeof SHOPIFY_API_SECRET === "string") {
return crypto.createHmac("sha256", SHOPIFY_API_SECRET).update(params as DataView).digest('hex');
} else {
throw new Error("during generateEncryptedHash() SHOPIFY_API_SECRET was not a string")
}
};
我不确定 DataView
是什么,但您只能使用字符串/缓冲区更新 digest
。所以你必须使用 JSON.stringify
如果输入是一个对象并且这就是你想要的哈希值,例如.update(JSON.stringify(params))
UPD:您应该为参数设置类型 params