Error: Cannot read property 'replace' of u

2019-01-30 14:20发布

问题:

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

And added iOS platform using cordova platform add iOS. And tried to do cordova run ios after cordova build ios.

But it shows me this error(I used --d/ --verbose to get the details).

/usr/bin/codesign --force --sign - --timestamp=none /Volumes/Untitled/Plot/PlotReel/platforms/ios/build/emulator/PlotReel.app /Volumes/Untitled/Plot/PlotReel/platforms/ios/build/emulator/PlotReel.app: replacing existing signature

** BUILD SUCCEEDED **

No scripts found for hook "before_deploy". Error: TypeError: Cannot read property 'replace' of undefined

at remove (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/ios-sim/src/lib.js:282:70)
at Array.forEach (native)
at Object.getdevicetypes (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/ios-sim/src/lib.js:292:22)
at Object.listEmulatorImages [as run] (/Volumes/Untitled/Plot/test/platforms/ios/cordova/lib/list-emulator-images:34:29)
at deployToSim (/Volumes/Untitled/Plot/test/platforms/ios/cordova/lib/run.js:146:50)
at /Volumes/Untitled/Plot/test/platforms/ios/cordova/lib/run.js:88:20
at _fulfilled (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/q/q.js:834:54)
at self.promiseDispatch.done (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/q/q.js:863:30)
at Promise.promise.promiseDispatch (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/q/q.js:796:13)
at /Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/q/q.js:604:44

I have tried uninstalling and installing cordova again, but the problem is still remaining.

Please help me.

回答1:

New solution

This issue is fixed in the latest version of the "ios-sim" package (so now this is probably the easier solution - compared to the old one which is listed below). In order to update the "ios-sim" package to the latest version run this in your terminal/cmd:

cd platforms/ios/cordova/node_modules/
sudo npm install -g ios-sim@latest

Old solution

The problem is that the name_id_map[deviceName] returns undefined for "iPad Pro (12.9-inch)" and "iPad Pro (10.5-inch)".

You can check it with a console.log('name_id_map[ deviceName ]: ' + name_id_map[ deviceName ]);.

I fixed this bug by adding an if statements which checks if the device is defined in "platforms/ios/cordova/node_modules/ios-sim/src/lib.js:282".

I replaced this:

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 /, '')));
        };

with this:

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

The "iPad Pro (10.5-inch)" simulator won't be on the list (but it doesn't probably work anyway - didn't check).

Bug report on github: https://github.com/phonegap/ios-sim/issues/210



回答2:

in your project folder root, do cd platforms/ios/cordova && npm install ios-sim



回答3:

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 /, '')));


回答4:

There is a PR on Github which fixed my problem: https://github.com/phonegap/ios-sim/pull/213

Just called following inside my project root

nano platforms/ios/cordova/node_modules/ios-sim/src/lib.js

and added the function to filter the device name, as pointed out here: https://github.com/phonegap/ios-sim/pull/213/files



回答5:

I recently upgreaded to xcode 8.3.3 and ionic 3.4.0

I have removed ios-sim directory from myApp/platforms/ios/cordova/node_modules and now it's working.



回答6:

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.



回答7:

I have just run into this and thought I'd add something that worked for me - the solution npm install ios-sim didn't.

All I did was open up XCode and it was pointing to Generic iOS Device from when I last used it with testing an app on a physical device. I just changed the iOS Simulator to be anything on the iOS Simulator list, retried it and it worked like a charm!

Hope this might help somebody else in the same situation.



回答8:

Updating the ios-sim version with npm install ios-sim@latest didn't worked for me. But if found a nice and easy solution on Github.

  1. Open /platforms/ios/cordova/node_modules/ios-sim/src/lib.js
  2. Search for deviceName with your code editor
  3. Replace name_id_map[ deviceName ] with name_id_map[filterDeviceName(deviceName)]

You can find the Github post here



回答9:

I have run following commands and it solves my problem:

cd project_dir

sudo npm install ios-sim@latest