Nativescript http post响应未定义

时间:2017-09-13 18:15:53

标签: angular http undefined response nativescript

我已经启动了一个Nativescript应用程序,我正在尝试使用http post请求连接到我的nodejs后端,但我的响应未定义+当我调用发出请求的服务时,我的subscribe()函数无法访问。 / p>

这里console.log返回正确的authKey。

app.post('/login', (request, response) => {
        const user = request.body;
        return data.login(user)
            .then((authKey) => {
                console.log(authKey)
                return response.json(authKey);
            })
            .catch((err) => {
                return response.json(err);
            });
    });

登录功能:

const login = (user) => {
        return db
            .collection('some collection')
            .findOne({ username: user.username })
            .then((foundUser) => {
                if (foundUser) {
                    if (bcrypt.compareSync(user.password, foundUser.password)) {
                        return Promise.resolve(foundUser.authKey);
                    }
                    else {
                        throw Error('Wrong input');
                    }
                }
            });
    }

这是我的服务,在那里我尝试了一些事情,例如使用map()来解析对json的响应,但这里的响应本身是未定义的。

login(user) {
        return this.http.post('http://myip:3000/login', user);
    }

我是如何使用它的。在这里,console.log最让人失望:

login() {
    const user = {
      username: this.username,
      password: this.password
    }
    return this.authService.login(user)
      .subscribe((authKey) => {
        console.log(authKey);
      });
  }

0 个答案:

没有答案