Error: Cannot read property 'replace' of undefined when building iOS Cordova

前端 未结 9 1383
醉话见心
醉话见心 2020-12-23 09:32

I created a cordova project using cordova create project hello com.hello Hello.

And added iOS platform using cordova platform add iOS. And

相关标签:
9条回答
  • 2020-12-23 10:23

    I had the same error. For me I traced this down into a bug in platforms/ios/cordova/node_modules/ios-sim/src/lib.js

    getdevicetypes: function(args) {
    ...
        list.devicetypes.forEach(function(device) {
            name_id_map[ filterDeviceName(device.name) ] = device.id;
        });
    
        list = [];
        var remove = function(runtime) {
            // remove "iOS" prefix in runtime, remove prefix "com.apple.CoreSimulator.SimDeviceType." in id
            list.push(util.format('%s, %s', name_id_map[ deviceName ].replace(/^com.apple.CoreSimulator.SimDeviceType./, ''), runtime.replace(/^iOS /, '')));
        };
    

    The error always occured as "TypeError: Cannot read property 'replace' of undefined" in lib.js:289

    list.push(util.format('%s, %s', name_id_map[ deviceName ].replace(/^com.apple.CoreSimulator.SimDeviceType./, ''), runtime.replace(/^iOS /, '')));
    

    So I inserted some debug code:

        list.devicetypes.forEach(function(device) {
            console.log('test 1 ' + device.name);
            console.log('test 2 ' + filterDeviceName(device.name));
            name_id_map[ filterDeviceName(device.name) ] = device.id;
        });
    

    This worked for me. Good luck.

        list = [];
        var remove = function(runtime) {
            // remove "iOS" prefix in runtime, remove prefix "com.apple.CoreSimulator.SimDeviceType." in id
            console.log('remove 1 ' + runtime);
            console.log('remove 2 ' + deviceName);
            console.log('remove 3 ' + name_id_map[ deviceName ]);
            list.push(util.format('%s, %s', name_id_map[ deviceName ].replace(/^com.apple.CoreSimulator.SimDeviceType./, ''), runtime.replace(/^iOS /, '')));
        };
    

    and got the following output:

    test 1 iPhone 5
    test 2 iPhone 5
    test 1 iPad Pro (9.7-inch)
    test 2 iPad Pro (9.7 inch)
    remove 1 iOS 10.2
    remove 2 iPhone 5
    remove 3 com.apple.CoreSimulator.SimDeviceType.iPhone-5
    remove 1 iOS 10.2
    remove 2 iPad Pro (9.7-inch)
    remove 3 undefined
    

    Notice how filterDeviceName removed the minus character while filling the hash. When the value is retrieved again, the filter is not applied and the program fails.

    Bug fix: apply the filter while writing to and reading from the hash.

     list.push(util.format('%s, %s', name_id_map[ filterDeviceName(deviceName) ].replace(/^com.apple.CoreSimulator.SimDeviceType./, ''), runtime.replace(/^iOS /, '')));
    
    0 讨论(0)
  • 2020-12-23 10:24

    I have run following commands and it solves my problem:

    cd project_dir

    sudo npm install ios-sim@latest

    0 讨论(0)
  • 2020-12-23 10:26

    Another option is to use a cordova-ios version with the ios-sim patch already implemented

    cordova platform add https://github.com/apache/cordova-ios.git#4.4.0-ios-sim

    Beware that this is not an official release of Apache Cordova, this will be included in next 4.4.1 version.

    0 讨论(0)
提交回复
热议问题