小编典典

Firestore 设置调用不适用于 safari

javascript

我正在尝试在 firestore 数据库上添加一个新文档,它可以在 Chrome 上运行,但在 Safari 上不行。我几乎尝试了所有方法,如您所见,我添加了这一行firebase.firestore().settings({ experimentalForceLongPolling: true });,但仍然没有用。浏览器缓存已清除…规则设置为 true 用于写入和读取。我可以读取数据,但无法在 SAFARI 上编写/添加新文档。

我收到此错误在此处输入图像描述

const firebaseConfig = {
          apiKey: "AIzaSyA34jId_vxluwcj1F9Zl-JeEeZiP0ip_eY",
          authDomain: "{PROJECT-NAME}.firebaseapp.com/",
          databaseURL: "https://{PROJECT-NAME}.firebaseio.com/",
          projectId: "{PROJECT-NAME}",
          storageBucket: "https://{PROJECT-NAME}.appspot.com/",
          messagingSenderId: "316955821458",
          appId: "1:316955821458:web:5916a8b13256ada9cc2d4b",
          measurementId: "G-JYCTHTXTVE"
};
if (!firebase.apps.length) {
  firebase.initializeApp(firebaseConfig);
}
// Firebase register
  $('#firebase-register-form-submit').click(function (event) {
    event.preventDefault();
    let data = $('#firebase-register-form :input').serializeArray();



    // Initialize Firebase
    firebase.firestore().settings({ experimentalForceLongPolling: true }); 
    let db = firebase.firestore();

    let email = data[0].value;
    let password = data[1].value;
    let name = data[2].value;
    let phoneNumber = data[3].value;
    let checkBox;
    // start login into firebase
    if (email !== '' && password !== '' && name !== '' && phoneNumber !== '') {
      firebase
        .auth()
        .createUserWithEmailAndPassword(email, password)
        .then(() => {
          if(data[4]) {
              checkBox = true;
          }
          else {
              checkBox = false;
          }

          //const newId = db.createId();
          let dataAdded = {
            email: email,
            isSubscribed: checkBox,
            name: name,
            phoneNumber: phoneNumber
            };
            let setDoc = db.collection("users").doc().set(dataAdded);


          window.location = 'http://www.womanverse.ro/membership';
        })
        .catch((error) => {
          console.log(error.message);
          $('p#firebase-register-error').show();
          $('p#firebase-register-error').text(error.message);
        });
    } else {
      $('p#firebase-register-error').text(
        'Vă rugăm să completați toate câmpurile!'
      );
    }
  });

阅读 106

收藏
2022-07-26

共1个答案

小编典典

set()在调用有时间完成之前,您的代码正在离开页面。正如您从链接的 API 文档中看到的那样,set()它是异步的并返回一个承诺。完成可能需要任何时间。当您设置 window.location 时,这会导致当前正在运行的 JavaScript 完全停止、卸载并加载一个全新的页面。在执行任何其他操作之前,您应该使用返回的 Promise 等待调用完成。

db.collection(“users”).doc().set(dataAdded)
.then(() => {
// set is complete
})
.catch(error => {
// handle the error
});

2022-07-26