Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dduo518 opened this issue Nov 17, 2017 · 0 comments
Open

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

dduo518 opened this issue Nov 17, 2017 · 0 comments

Comments

@dduo518
Copy link

dduo518 commented Nov 17, 2017

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;
                }
            }

直接判断是否存在就可以

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant