【问题标题】:Cordova: Invalid data, chunk must be a string or buffer, not objectCordova:无效数据,块必须是字符串或缓冲区,而不是对象
【发布时间】:2017-07-25 05:17:06
【问题描述】:

我更新到 Ionic 3.5,之后我得到这个错误,当我尝试做 cordova build ios:

Invalid data, chunk must be a string or buffer, not object

没有解释为什么会发生这个错误。我用 Cordova 7.0.1 和 6.5.0 都试过了。有趣的是,它适用于 Windows 机器,但不适用于 Mac。我只在 Mac 上得到错误。感谢您提供任何见解或帮助。

 ionic info

全局包:

@ionic/cli-utils : 1.5.0
Cordova CLI      : 7.0.1
Ionic CLI        : 3.5.0

本地包:

@ionic/app-scripts              : 1.3.7
@ionic/cli-plugin-cordova       : 1.4.1
@ionic/cli-plugin-ionic-angular : 1.3.2
Cordova Platforms               : android 6.2.3
Ionic Framework                 : ionic-angular 3.5.3

系统:

Node       : v7.10.0
OS         : Windows 10
Xcode      : not installed
ios-deploy : not installed
ios-sim    : not installed
npm        : 4.6.1

【问题讨论】:

    标签: ios cordova ionic2 cordova-plugins


    【解决方案1】:

    我从github issue 之一获得了修复。它只需要 strings.xml 的正确路径。

    无需降级cordova或cordova-android

    修复方法是替换中的代码 /cordova-plugin-fcm/scripts/fcm_config_files_process.js如下:

    #!/usr/bin/env node
    'use strict';
    
    var fs = require('fs');
    var path = require('path');
    
    fs.ensureDirSync = function (dir) {
        if (!fs.existsSync(dir)) {
            dir.split(path.sep).reduce(function (currentPath, folder) {
                currentPath += folder + path.sep;
                if (!fs.existsSync(currentPath)) {
                    fs.mkdirSync(currentPath);
                }
                return currentPath;
            }, '');
        }
    };
    
    var config = fs.readFileSync('config.xml').toString();
    var name = getValue(config, 'name');
    
    var IOS_DIR = 'platforms/ios';
    var ANDROID_DIR = 'platforms/android';
    
    var PLATFORM = {
        IOS: {
            dest: [
                IOS_DIR + '/' + name + '/Resources/GoogleService-Info.plist',
                IOS_DIR + '/' + name + '/Resources/Resources/GoogleService-Info.plist'
            ],
            src: [
                'GoogleService-Info.plist',
                IOS_DIR + '/www/GoogleService-Info.plist',
                'www/GoogleService-Info.plist'
            ]
        },
        ANDROID: {
            dest: [
                ANDROID_DIR + '/google-services.json',
                ANDROID_DIR + '/app/google-services.json',
            ],
            src: [
                'google-services.json',
                ANDROID_DIR + '/assets/www/google-services.json',
                'www/google-services.json'
            ],
            stringsXml: ANDROID_DIR + '/app/src/main/res/values/strings.xml'
        }
    };
    
    // Copy key files to their platform specific folders
    if (directoryExists(IOS_DIR)) {
        copyKey(PLATFORM.IOS);
    }
    if (directoryExists(ANDROID_DIR)) {
        copyKey(PLATFORM.ANDROID, updateStringsXml)
    }
    
    function updateStringsXml(contents) {
        var json = JSON.parse(contents);
        var strings = fs.readFileSync(PLATFORM.ANDROID.stringsXml).toString();
    
        // strip non-default value
        strings = strings.replace(new RegExp('<string name="google_app_id">([^\@<]+?)</string>', 'i'), '');
    
        // strip non-default value
        strings = strings.replace(new RegExp('<string name="google_api_key">([^\@<]+?)</string>', 'i'), '');
    
        // strip empty lines
        strings = strings.replace(new RegExp('(\r\n|\n|\r)[ \t]*(\r\n|\n|\r)', 'gm'), '$1');
    
        // replace the default value
        strings = strings.replace(new RegExp('<string name="google_app_id">([^<]+?)</string>', 'i'), '<string name="google_app_id">' + json.client[0].client_info.mobilesdk_app_id + '</string>');
    
        // replace the default value
        strings = strings.replace(new RegExp('<string name="google_api_key">([^<]+?)</string>', 'i'), '<string name="google_api_key">' + json.client[0].api_key[0].current_key + '</string>');
    
        fs.writeFileSync(PLATFORM.ANDROID.stringsXml, strings);
    }
    
    function copyKey(platform, callback) {
        for (var i = 0; i < platform.src.length; i++) {
            var file = platform.src[i];
            if (fileExists(file)) {
                try {
                    var contents = fs.readFileSync(file).toString();
    
                    try {
                        platform.dest.forEach(function (destinationPath) {
                            var folder = destinationPath.substring(0, destinationPath.lastIndexOf('/'));
                            fs.ensureDirSync(folder);
                            fs.writeFileSync(destinationPath, contents);
                        });
                    } catch (e) {
                        // skip
                    }
    
                    callback && callback(contents);
                } catch (err) {
                    console.log(err)
                }
    
                break;
            }
        }
    }
    
    function getValue(config, name) {
        var value = config.match(new RegExp('<' + name + '>(.*?)</' + name + '>', 'i'));
        if (value && value[1]) {
            return value[1]
        } else {
            return null
        }
    }
    
    function fileExists(path) {
        try {
            return fs.statSync(path).isFile();
        } catch (e) {
            return false;
        }
    }
    
    function directoryExists(path) {
        try {
            return fs.statSync(path).isDirectory();
        } catch (e) {
            return false;
        }
    }
    

    解决问题:

    确保您已将上述文件复制到“plugins”文件夹中,因为 cordova 将所有 cordova-plugins 从 node_modules 目录复制到 plugins 目录,

    如果您在修改文件之前已经添加了平台 plugins/cordova-plugin-fcm/scripts/fcm_config_files_process.js,您需要删除平台并重新添加。

    【讨论】:

      【解决方案2】:

      @Ari 如果您仍然遇到此问题,这就是我为解决此问题所做的。

      我必须编辑位于文件夹“plugins/cordova-plugin-fcm/scripts/”中的文件“fcm_config_files_process.js”:

      // fs.writeFileSync("platforms/ios/" + name + "/Resources/GoogleService-Info.plist", contents)
      

      在构建项目时出于某种未知原因,这行 (42) 抛出错误“无效数据,块必须是字符串或缓冲区,而不是对象”,所以我所做的是注释该行,然后手动复制文件“GoogleService-Info.plist”到“platforms/ios/”+名称+“/Resources/”

      希望对您有所帮助。

      【讨论】:

        【解决方案3】:

        我们遇到此错误是因为我们来自 Apple 的开发推送证书已过时。我们已经更新了它——而且它奏效了。

        【讨论】:

          【解决方案4】:

          创建 Ionic 2 应用程序时删除默认添加的 ios 平台,然后再次添加。
          并删除并添加“cordova-fcm-plugin”。
          离子平台 rm ios
          离子平台添加ios

          【讨论】:

            猜你喜欢
            • 2023-04-04
            • 2018-08-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-02-17
            • 2022-07-14
            • 2015-02-28
            • 1970-01-01
            相关资源
            最近更新 更多