简体   繁体   English

类型错误:fpPromise.then 不是 function

[英]TypeError: fpPromise.then is not a function

I want to using fingerprintjs to get the device id.我想使用fingerprintjs来获取设备 ID。 this is the typescript code looks like:这是 typescript 代码,如下所示:

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;

when I run this code, shows error:当我运行这段代码时,显示错误:

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

I traced the code and found fpPromise is not null. why did this happen?查了一下代码发现fpPromise不是null,这是为什么? how to fixed this problem?如何解决这个问题? the fingerprintjs version is: fingerprintjs 版本是:

"@fingerprintjs/fingerprintjs": "^3.3.2",

this is how to invoke this function in typescript "ttypescript": "^1.5.13", :这是在 typescript "ttypescript": "^1.5.13",中调用这个 function 的方法:

import DeviceHandler from "@utils/data/DeviceHandler";
const deviceId = await DeviceHandler.getDeviceId();

the node version is v16.13.2 .节点版本是v16.13.2

Importing a module inline using import() will return Promise<FingerprintJS> (because it's asynchronous).使用import()内联导入模块将返回Promise<FingerprintJS> (因为它是异步的)。 Importing a module inline using require() will return FingerprintJS directly (because it's synchronous).使用require()内联导入模块将直接返回FingerprintJS (因为它是同步的)。

For your application, to match the documentation's first sample , you should replace:对于您的应用程序,为了匹配文档的第一个示例,您应该替换:

// here, fpPromise is a FingerprintJS object - not a Promise!
const fpPromise = require('@fingerprintjs/fingerprintjs');

with

// here, fpPromise is a Promise<Agent> object
const fpPromise = require('@fingerprintjs/fingerprintjs').load();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM