错误:构建 iOS Cordova 时无法读取未定义的属性“替换”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42350505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: Cannot read property 'replace' of undefined when building iOS Cordova
提问by Eiji Kimura
I created a cordova project using cordova create project hello com.hello Hello
.
我使用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
.
并使用cordova platform add iOS
. 并试图做到cordova run ios
后cordova build ios
。
But it shows me this error(I used --d/ --verbose to get the details).
但它向我显示了这个错误(我使用 --d/ --verbose 来获取详细信息)。
/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
/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:替换现有签名
** 构建成功 **
未找到挂钩“before_deploy”的脚本。错误:类型错误:无法读取未定义的属性“替换”
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.
我再次尝试卸载和安装cordova,但问题仍然存在。
Please help me.
请帮我。
回答by Tadej
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:
这个问题在最新版本的“ios-sim”包中得到修复(所以现在这可能是更简单的解决方案 - 与下面列出的旧版本相比)。为了将“ios-sim”包更新到最新版本,请在您的终端/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)".
问题是“ iPad Pro(12.9 英寸)”和“ iPad Pro(10.5 英寸)”的name_id_map[deviceName]
退货。undefined
You can check it with a console.log('name_id_map[ deviceName ]: ' + name_id_map[ deviceName ]);
.
您可以使用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".
我通过添加一个 if 语句来修复这个错误,该语句检查设备是否在“ 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).
“iPad Pro(10.5 英寸)”模拟器不会在列表中(但它可能无论如何都不起作用 - 没有检查)。
Bug report on github: https://github.com/phonegap/ios-sim/issues/210
github 上的错误报告:https: //github.com/phonegap/ios-sim/issues/210
回答by Reza
in your project folder root, do cd platforms/ios/cordova && npm install ios-sim
在您的项目文件夹根目录中,执行 cd platforms/ios/cordova && npm install ios-sim
回答by mschmitt
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
我有同样的错误。对我来说,我将其追溯到平台/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
错误总是在 lib.js:289 中出现为“TypeError:无法读取未定义的属性‘替换’”
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.
请注意 filterDeviceName 如何在填充散列时删除减号。当再次检索该值时,不会应用过滤器并且程序失败。
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 /, '')));
回答by Hirbod
There is a PR on Github which fixed my problem: https://github.com/phonegap/ios-sim/pull/213
Github 上有一个 PR 解决了我的问题: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
并添加了过滤设备名称的功能,如下所示:https: //github.com/phonegap/ios-sim/pull/213/files
回答by chintan adatiya
I recently upgreaded to xcode 8.3.3 and ionic 3.4.0
我最近升级到 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.
我已经从 myApp/platforms/ios/cordova/node_modules 中删除了 ios-sim 目录,现在它可以工作了。
回答by CYMR0
I have just run into this and thought I'd add something that worked for me - the solution npm install ios-sim
didn't.
我刚刚遇到这个问题,并认为我会添加一些对我有用的东西 - 解决方案npm install ios-sim
没有。
All I did was open up XCode and it was pointing to Generic iOS Devicefrom 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!
我所做的只是打开 XCode,它指向我上次在物理设备上测试应用程序时使用的通用 iOS 设备。我只是将 iOS 模拟器更改为 iOS 模拟器列表中的任何内容,重试了它,它就像一个魅力!
Hope this might help somebody else in the same situation.
希望这可以帮助处于相同情况的其他人。
回答by Eyk Rehbein
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.
更新 ios-sim 版本npm install ios-sim@latest
对我不起作用。但是,如果在 Github 上找到了一个不错且简单的解决方案。
- Open
/platforms/ios/cordova/node_modules/ios-sim/src/lib.js
- Search for
deviceName
with your code editor - Replace
name_id_map[ deviceName ]
withname_id_map[filterDeviceName(deviceName)]
- 打开
/platforms/ios/cordova/node_modules/ios-sim/src/lib.js
deviceName
使用您的代码编辑器搜索- 替换
name_id_map[ deviceName ]
为name_id_map[filterDeviceName(deviceName)]
You can find the Github post here
你可以在这里找到 Github 帖子
回答by jcesarmobile
Another option is to use a cordova-ios
version with the ios-sim
patch already implemented
另一种选择是使用已实施补丁的cordova-ios
版本ios-sim
cordova platform add https://github.com/apache/cordova-ios.git#4.4.0-ios-sim
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.
请注意,这不是 Apache Cordova 的正式版本,它将包含在下一个 4.4.1 版本中。
回答by Deepak Bhatta
I have run following commands and it solves my problem:
我运行了以下命令,它解决了我的问题:
cd project_dir
cd project_dir
sudo npm install ios-sim@latest
须藤 npm 安装 ios-sim@latest