javascript - AJAX get 请求无法识别提交的数据?

标签 javascript firebase firebase-realtime-database firebase-authentication

我在客户端运行以下代码以访问数据库中用户的属性。

firebaseAUTH.signInWithEmailAndPassword(email, password).then(function (user) {
console.log('user has signed in with e-mail address: '+user.email+' and user ID: '+user.uid)
firebaseAUTH.currentUser.getToken(true).then(function(idToken) {
$.ajax(
    {
// Send token to your backend via HTTPS (JWT)
      url: '/auth',
      type: 'POST',
      data: {token: idToken},
      success: function (response){
      var userID = response.userID
  firebase.database().ref('/users/' + userID).once('value').then(function(snapshot) {
  var industry = snapshot.val().industry
  var company = snapshot.val().company
  var firstname = snapshot.val().firstname
  var email = snapshot.val().email
  var source = snapshot.val().source
  var phone = snapshot.val().phone
  var password = snapshot.val().password
  var lastname = snapshot.val().lastname
      $.get(
        {
          url: '/members-area/'+userID,
          data: {userID: userID,
                industry: industry,
                email: email},
          success: function(response){
          window.location = '/members-area/'+userID
          }
      })

我的服务器端代码:

app.get('/members-area/:userID', function(req,res,next) {
  res.render('members-area', { userID: req.params.userID, industry: req.params.industry, email: req.params.email})                                                                  
})

但是,当我尝试访问 pug 中的 'industry' 变量时,它显示未定义。如您所见,我在上面的 GET ajax 调用中发送了它,那么问题是什么?这也很奇怪,因为我在函数快照之后立即将变量名称记录到控制台,它们就在那里。此外,神秘的“userID”显示为包含内容的 var,但“industry”和“email”根本没有。

最佳答案

我不太清楚你想做什么,但希望我能帮到你一些。

首先,您不需要第二次调用来获取 token 。当您调用 signInWithEmailAndPassword 时,firebase 会返回用户。所以你可以马上调用getToken

firebaseAUTH.signInWithEmailAndPassword(email, password).then(function (user) {
console.log('user has signed in with e-mail address: '+user.email+' and user ID: '+user.uid)
console.log('we also got the token: ' + user.getToken());
...

您似乎也发布到未定义的路由,然后您使用 get 查询不同的路由。

Also, mysteriously 'userID' shows up as a var with content but 'industry' and 'email' do not at all.

在您的服务器端代码中,您的路由仅使用一个参数定义:userID。线路

app.get('/members-area/:userID', function(req,res,next) 

将 userID 定义为参数,而不是其他 2 个变量。所以它们未定义是有道理的。

我认为您想要做的是:

firebaseAUTH.signInWithEmailAndPassword(email, password).then(function (user) {
    const userId = user.getToken();
    firebase.database().ref('/users/' + userID).once('value').then(function(snapshot) {
        $.post('/members-area/' + userId, snapshot.val(), function(data, status) {
           console.log(data);
           console.log(status);
    });
});

然后在您的服务器代码中:

app.post('/members-area/:userID', function(req,res,next) {
  const theSnapshot = req.body;
  res.send(theSnapshot)
});

我仍然不明白为什么您要使用客户端代码从数据库中检索信息,然后将其发布到服务器,只是为了再次获取它。但也许我误解了什么:)

看到发送数据的 get 请求也很奇怪,我很确定它不符合规范。您通常希望使用 post 发送数据,然后使用 get 来 ehm 获取数据:)

关于javascript - AJAX get 请求无法识别提交的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43836855/

相关文章:

javascript - 仅使用 javascript 获取 JSON 并评估数据

javascript 验证格式为 hh :mm a 的输入时间

firebase - Firebase 数据库触发器的云功能?

android - 评估项目 ':app'时发生问题。 > IntelliJ IDEA上用于Android应用程序开发的ASCII uisng kotlin

Firebase RTDB 在关键字列表中搜索关键字

javascript - Mocha 和 Chai 未能兑现 promise

javascript - Chrome Dev Tools 命中代码但没有断点

android - FirebaseInstanceId - 每个 Firebase "App"或 Android 应用程序唯一?

android - 将 Firebase Persistence Enabled 设置为 true 后无法检索新数据?

java - 为了过滤时间,我可以在 firebase 实时数据库中做什么?