UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:60):RangeError:无效的状态代码:0

时间:2017-01-16 22:43:50

标签: java

使用node.js和mongodb创建一个简单的登录应用程序。创建用户(通过电子邮件和密码)有效,但我想使用相同的用户名和密码登录。终端输出(在调用节点app.js之后)显示以下错误;

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 60): RangeError: Invalid status code: 0

这是login.js

'use strict';

const user = require('../models/user');
const bcrypt = require('bcryptjs');

exports.loginUser = (email, password) =>
    new Promise((resolve,reject) => {
        user.find({email: email})
        .then(users => {
            if (users.length == 0) {
                reject({ status: 404, message: 'User Not Found !' });
            } else {
                return users[0];
            }
        })
        .then(user => {
            const hashed_password = user.hashed_password;
            if (bcrypt.compareSync(password, hashed_password)) {
                resolve({ status: 200, message: email });
            } else {
                reject({ status: 401, message: 'Invalid Credentials !' });
            }
        })
        .catch(err => reject({ status: 500, message: 'Internal Server Error!' }));
    });

调试代码后,我意识到LoginFragment.java类涉及登录过程。以下是必要的方法:

private void login(){

    setError();

    String email = mEtEmail.getText().toString();
    String password = mEtPassword.getText().toString();

    int err = 0;

    if (!validateEmail(email)) {

        err++;
        mTiEmail.setError("Email should be valid !");
    }

    if (!validateFields(password)) {

        err++;
        mTiPassword.setError("Password should not be empty !");
    }

    if (err == 0) {

        loginProcess(email,password);
        mProgressBar.setVisibility(View.VISIBLE);

    } else {

        showSnackBarMessage("Enter Valid Details !");
    }
}

private void setError() {

    mTiEmail.setError(null);
    mTiPassword.setError(null);
}

private void loginProcess(String email, String password) {

    mSubscriptions.add(NetworkUtil.getRetrofit(email, password).login()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(this::handleResponse,this::handleError));
}

private void handleResponse(Response response) {

    mProgressBar.setVisibility(View.GONE);

    SharedPreferences.Editor editor = mSharedPreferences.edit();
    editor.putString(Constants.TOKEN,response.getToken());
    editor.putString(Constants.EMAIL,response.getMessage());
    editor.apply();

    mEtEmail.setText(null);
    mEtPassword.setText(null);

    Intent intent = new Intent(getActivity(), ProfileActivity.class);
    startActivity(intent);

}

private void handleError(Throwable error) {

    mProgressBar.setVisibility(View.GONE);

    if (error instanceof HttpException) {

        Gson gson = new GsonBuilder().create();

        try {

            String errorBody = ((HttpException) error).response().errorBody().string();
            Response response = gson.fromJson(errorBody,Response.class);
            showSnackBarMessage(response.getMessage());

        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {

        showSnackBarMessage("Network Error !");
    }
}

private void showSnackBarMessage(String message) {

    if (getView() != null) {

        Snackbar.make(getView(),message,Snackbar.LENGTH_SHORT).show();
    }
}

我是Node.js的新手,所以我不知道如何调试它并发现错误。有人能指出我正确的方向吗?

0 个答案:

没有答案