我想使用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/fingerprintjs": "^3.3.2",
以下是如何在类型脚本"ttypescript": "^1.5.13",
:中调用此函数
import DeviceHandler from "@utils/data/DeviceHandler";
const deviceId = await DeviceHandler.getDeviceId();
节点版本为CCD_ 3。
使用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();