Skip to content

传入英文翻译目标也为英文时会报报错 #3

@dduo518

Description

@dduo518
const translate = require('translate-api');
let transText = 'hello world';
translate.getText(transText, { to: 'en' }).then(function(text) {
    console.log(text)
}).catch(err => {
    console.log(err)  // {Error code: 'BAD_NETWORK' }
});

真实错误信息是

TypeError: Cannot read property '0' of null

api.js修改成这样就好了

// if (body[5] !== undefined && body[5][0] !== undefined && body[5][0][0] !== undefined) {
            if (body[5] && body[5][0] && body[5][0][0]) {
                let str = body[5][0][0];

                str = str.replace(/<b><i>/g, '[');
                str = str.replace(/<\/i><\/b>/g, ']');

                result.from.text.value = str;

                if (body[5][0][5] === 1) {
                    result.from.text.autoCorrected = true;
                } else {
                    result.from.text.didYouMean = true;
                }
            }

直接判断是否存在就可以

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions