Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firestore验证日期(参数“秒”的值必须在[-62135596800、253402300799]范围内,包括在内)_Javascript_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript Firestore验证日期(参数“秒”的值必须在[-62135596800、253402300799]范围内,包括在内)

Javascript Firestore验证日期(参数“秒”的值必须在[-62135596800、253402300799]范围内,包括在内),javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,我正在对firebase进行批量写入,同时创建一个带有电子邮件的用户 try { // Get a new Firestore batched write const batch = firestore.batch(); // Generate an user uuid const userId = uuidv4(); // Create the new username document in the usernames collection

我正在对firebase进行批量写入,同时创建一个带有电子邮件的用户

   try {
    // Get a new Firestore batched write
    const batch = firestore.batch();

    // Generate an user uuid
    const userId = uuidv4();

    // Create the new username document in the usernames collection with the user's id as field
    const usernameRef = firestore.collection("usernames").doc(username);
    batch.set(usernameRef, { userId });

    // Create the new user document in the users collection with all the relevant information
    const userRef = firestore.collection("users").doc(userId);

    // Pass the username to lower case
    username = username.toLowerCase();

    // Take the birthday milliseconds and convert it back to a Date
    birthday = new Date(birthday);

    const data = {
      email,
      username,
      name,
      birthday,
    };

    batch.set(userRef, data);

    // Create the user with Firebase Authentication
    await admin.auth().createUser({
      uid: userId,
      email,
      password,
    });

    // Commit the batch
    await batch.commit(); 

  } catch(err) {
    ...
  }
这样,如果电子邮件已被接收,则不会将用户添加到数据库中。但有一个问题,如果Firestore的生日无效,则会在批提交中捕获异常。。。因此,电子邮件将被注册

在将日期值提交到firestore之前,是否有任何方法验证日期值?例如,如果我

  birthday = new Date(-182397382409)
这将抛出一个错误

参数“秒”的值必须在[-62135596800、253402300799]范围内(包括在内)

此外,如果在批处理写入中也包含admin.createUser,则问题将得到解决,但我不知道这是否可行


谢谢。

为了避免在db和身份验证电子邮件中创建用户,我验证了所有客户端表单,然后运行问题中的代码

这是表单验证
为了避免在db和身份验证电子邮件中创建用户,我验证了所有客户端表单,然后运行问题中的代码

这是表单验证
您可以验证
生日
问题是firestore自动将js日期转换为{seconds:(不是date.getSeconds()),纳秒:}对象。我不知道如何验证js日期,使firestore对象的秒数介于[-62135596800,253402300799]之间。将毫秒js日期除以1000?@DougStevenson确定,但一秒钟只有1000毫秒。。。javascript日期以毫秒为单位。。。上面的代码是
生日=新日期(生日)
。。。因此,
生日
(在转换为日期对象之前)必须以毫秒为单位,而不是以纳秒为单位-我的建议是在将其转换为日期之前验证
生日
。。。因此,实际上必须确保
生日/1000
在允许的范围内(因为
的“秒”必须在[-62135596800,253402300799]
之内)@JaromandaX听起来像是一个答案。你可以验证
生日
问题是firestore自动转换js日期到{seconds:(不是date.getSeconds()),纳秒:}对象我不知道如何验证js日期,使firestore对象的秒数介于[-62135596800,253402300799]之间。将毫秒js日期除以1000?@DougStevenson确定,但一秒钟只有1000毫秒。。。javascript日期以毫秒为单位。。。上面的代码是
生日=新日期(生日)
。。。因此,
生日
(在转换为日期对象之前)必须以毫秒为单位,而不是以纳秒为单位-我的建议是在将其转换为日期之前验证
生日
。。。因此,实际上必须确保
生日/1000
在允许的范围内(因为
的“秒”必须在[-62135596800253402300799]
之内)。那么,JaromandaX听起来像是一个答案。
async function validateForm(
  username,
  email,
  password,
  repeatPassword,
  name,
  birthday,
  clientIp
) {
  ... Validate data types, availabilities in the db, regex (including password strength), ...

  // Validate the user's birthday
  if (!validateBirthday(birthday)) {
    return "The birthday is not valid";
  }
}

validateBirthday = function (birthday) {
  /* Birthday has to be in milliseconds since unix epoch */

  // Get the birthday in seconds
  const birthdayInSeconds = birthday / 1000;

  return birthdayInSeconds >= -62135596800 && birthdayInSeconds <= 253402300799
}
// Validate the form data
const feedback = await validateForm(
  username,
  email,
  password,
  repeatPassword,
  name,
  birthday,
  clientIp
);

// If the form has been validated and no feedback has been received...
if (!feedback) {
  try {
    // Get a new Firestore batched write
    const batch = firestore.batch();

    // Generate an user uuid
    const userId = uuidv4();

    // Create the new username document in the usernames collection with the user's id as field
    const usernameRef = firestore.collection("usernames").doc(username);
    batch.set(usernameRef, { userId });

    // Create the new user document in the users collection with all the relevant information
    const userRef = firestore.collection("users").doc(userId);

    // Pass the username to lower case
    username = username.toLowerCase();

    // Take the birthday milliseconds and convert it back to a Date
    birthday = new Date(birthday);

    const data = {
      email,
      username,
      name,
      birthday,
    };

    batch.set(userRef, data);

    // Create the user with Firebase Authentication
    await admin.auth().createUser({
      uid: userId,
      email,
      password,
    });

    // Commit the batch
    await batch.commit(); 

  } catch(err) {
    ...
  }
} else {
   // Throw feedback to the user
}