节点js后端返回未定义的值

时间:2019-11-23 20:41:40

标签: javascript node.js express backend

enter image description here试图将错误请求发送回客户端,但客户端js无法正确发送

它应该报告为成功或错误消息,以便我可以发送给客户端。

 class FirebaseHelper    
 {
//universal SUCCESS or ERROR message to send to client


//register user with email and password


register(email,password,username,fullname,db)
{ 

   //firebase auth create user

  const p = admin.auth().createUser({
    email: email,
    emailVerified: false,
    password: password,         
    })
    .then(() => {
      // on successs
      let docRef = db.collection('users').doc(email);

      let setAda = docRef.set({
        username: username,
        fullname: fullname,

      })
      var ResponseMessage = new String();
      ResponseMessage = '';
      console.log('Successfully created new user:', email)
      ResponseMessage = handleresponse('Success');
      return ResponseMessage;
    })
    .catch((error) => {
      //on failure
      var ResponseMessage = new String();
      ResponseMessage = '';
      ResponseMessage = handleresponse(`${error.message}`);
      return ResponseMessage;

    }); 
   return p

};  

function handleresponse(R)
{

ResponseMessage = R;
console.log(`${ResponseMessage}`)
return ResponseMessage;
};

这是我获得成功响应或错误响应的响应

这是main.js,我在其中获取请求,然后将其发送回客户端

const admin = require('firebase-admin');
const express = require('express');
var bodyParser = require('body-parser');

 const FirebaseHelper = require('./FirebaseHelper')
 const FirebaseHelper1 = new FirebaseHelper();

[![console][1]][1]//create express service
 const app = express()
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
module.exports = app;

  app.post('/register', (request, response) =>{
  console.log('request tried')
  success = new Boolean;
  REQUEST_DATA = request.body;

   if(REQUEST_DATA!== null){
    console.log(REQUEST_DATA);
    REQUEST_EMAIL = REQUEST_DATA.email;
    REQUEST_PASSWORD = REQUEST_DATA.password;
    REQUEST_USERNAME = REQUEST_DATA.username;
    REQUEST_FULLENAME = REQUEST_DATA.fullname;
    var RESPONSE = new String();

   RESPONSE = FirebaseHelper1.register(REQUEST_EMAIL,
REQUEST_PASSWORD,
REQUEST_USERNAME,
REQUEST_FULLENAME,db);

  if(RESPONSE === 'Success'){
    response.send({
      success: true,
      info: `sucess`
    });
  }
  else{
    response.send({
      success: false,
      info: `${RESPONSE}`
    });
  };


}
else{
console.log('request was null');

};

})

这就是我向用户显示的方式

 async private void registerUser_Clicked(object sender, EventArgs e)
    {
        RegisterUser response;
        string JSONresponse = await 
  apiRequestHelper.RequestRegisterUserAsync((RinputEmail.Text).ToString(), 
  (RinputPassword.Text).ToString(), (RinputUsername.Text).ToString(), 
(RinputFullName.Text).ToString());
         response = jsonHelper.deserializeRegisterResponse(JSONresponse);
        if (response.success == true) {
            await DisplayAlert("Alert", response.info, "OK");
            await Navigation.PushAsync(new Login());
        };
        if (response.success == false)
        {

            await DisplayAlert("Alert", response.info, "OK");
        }
        else {

            await DisplayAlert("Alert", "idk man", "OK");

        };
    }

注册用户类别

namespace Kula.Models
{
public class RegisterUser
{
  public bool success { get; set; }
  public string info { get; set; }
}
}

布尔值返回true或false,但是字符串始终是未定义的,请帮助几个小时。

用户外观

updated photo

1 个答案:

答案 0 :(得分:0)

好吧,实际上,更新后的图片可以清除情况,并且我相信您返回的是promise而不是实际的value。 在您的register函数中,删除return p,它应该可以工作。