UnhandledPromiseRejectionWarning:TypeError:无法设置未定义的属性“值”

我是JS的新手,并尝试了解我在代码中使用的Promise模块。下面是代码[1],在其中我试图通过提供几个value参数选项使其以某种方式运行,实际上我希望将其设置为cipher.encrypt(IP)(该示例提供了一种保留IP加密的格式,例如:cipher.encrypt(15.10.23.4)= 20.24.35.9)。 我收到的错误如下:

(node:9964) UnhandledPromiseRejectionWarning: TypeError: Cannot set property 'value' of undefined
    at cache.<computed>.promise (/home/pooja/Documents/weiterbildung/promises/simple.js:13:23)
    at new Promise (<anonymous>)
    at doEncryption (/home/pooja/Documents/weiterbildung/promises/simple.js:8:15)
    at Object.<anonymous> (/home/pooja/Documents/weiterbildung/promises/simple.js:29:12)
    at Module._compile (internal/modules/cjs/loader.js:1176:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

提前致谢,

[1]

const crypto = require('./fpe');                                                                                                                                                                                                   
const cipher = crypto({});                                                                                                                                                                                                         
const cache = {};                                                                                                                                                                                                                  

function doEncryption(IP) {                                                                                                                                                                                                        
    if (!cache[IP]) {                                                                                                                                                                                                              
        cache[IP] = {                                                                                                                                                                                                              
            promise: new Promise((resolve, reject) => {                                                                                                                                                                            

                if(cipher.encrypt(IP)) {                                                                                                                                                                                           
                    const value = "" + 10;  // const value = cipher.encrypt(IP)                                                                                                                       
                    cache[IP].value = value;                                                                                                                                                                                          
                    resolve(value);                                                                                                                                                                                                
                }                                                                                                                                                                                                                  
                else {                                                                                                                                                                                                             
                    resolve([]);                                                                                                                                                                                                   
                }                                                                                                                                                                                                                  
            }), //end of promise                                                                                                                                                                                                   
        };                                                                                                                                                                                                                         
        return cache[IP].promise;                                                                                                                                                                                                  
    } else if (!cache[IP].value) {                                                                                                                                                                                                 
        return cache[IP].promise;                                                                                                                                                                                                  
    }                                                                                                                                                                                                                              
    return Promise.resolve(cache[IP].value);                                                                                                                                                                                       
}                                                                                                                                                                                                                                  

IP = "10.10.10.10"                                                                                                                                                                                                                 
let temp = doEncryption(IP)                                                                                                                                                                                                        

temp.then(value => console.log(value))                                                                                                                                                                                             


评论
  • 回忆终止
    回忆终止 回复

    cache[IP] does not yet exist inside the promise. Furthermore, your code should not work, because you assign a new object to cache[IP] with only the promise (without value).

    尽管可能不是最佳的解决方法,但一种简单的修复方法可能是:

    cache[IP] = {}
    cache[IP].promise = ...