我想使用fingerprintjs來獲取設備ID。這是打字稿代碼的樣子:
const DeviceHandler = {
getDeviceId: async (): Promise<string> => {
return new Promise((resolve, reject) => {
// Initialize an agent at application startup.
const fpPromise = require('@fingerprintjs/fingerprintjs');
// Get the visitor identifier when you need it.
fpPromise
.then((fp: { get: () => any; }) => fp.get())
.then(async (result: { visitorId: any; }) => {
// This is the visitor identifier:
const deviceId = result.visitorId;
resolve(deviceId);
});
});
}
};
export default DeviceHandler;
當我運行此代碼時,顯示錯誤:
background.js:5253 Uncaught (in promise) TypeError: fpPromise.then is not a function
at background.js:5253:26
at new Promise (<anonymous>)
at background.js:5248:35
at step (background.js:5240:23)
at Object.next (background.js:5221:53)
at background.js:5215:71
at new Promise (<anonymous>)
at background.js:5211:12
at Object.getDeviceId (background.js:5246:39)
at background.js:4811:108
我跟蹤了代碼,發現fpPromise它不為空。為什么會這樣?如何解決這個問題?指紋版本是:
"@fingerprintjs/fingerprintjs": "^3.3.2",
這是在 typescript 中呼叫此函式的方法"ttypescript": "^1.5.13",:
import DeviceHandler from "@utils/data/DeviceHandler";
const deviceId = await DeviceHandler.getDeviceId();
節點版本是v16.13.2.
uj5u.com熱心網友回復:
使用行內匯入模塊import()將回傳Promise<FingerprintJS>(因為它是異步的)。使用行內匯入模塊require()將FingerprintJS直接回傳(因為它是同步的)。
對于您的應用程式,要匹配檔案的第一個示例,您應該替換:
// here, fpPromise is a FingerprintJS object - not a Promise!
const fpPromise = require('@fingerprintjs/fingerprintjs');
和
// here, fpPromise is a Promise<Agent> object
const fpPromise = require('@fingerprintjs/fingerprintjs').load();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/428573.html
標籤:打字稿
上一篇:如何使用TypeScript模擬useUser這個自定義鉤子
下一篇:“TypeError:無法讀取未定義的屬性'管道'”-我是否錯過了單元測驗中的其他設定(Angular/Unittesting/ActivatedRoute)
