簡體   English   中英

類型錯誤:fpPromise.then 不是 function

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

我想使用fingerprintjs來獲取設備 ID。 這是 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;

當我運行這段代碼時,顯示錯誤:

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不是null,這是為什么? 如何解決這個問題? fingerprintjs 版本是:

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

這是在 typescript "ttypescript": "^1.5.13",中調用這個 function 的方法:

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

節點版本是v16.13.2

使用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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM