0

This all my code:

lib.create = (dir, file, data, callback) => {
  fs.open(`${lib.basedir + dir}/${file}.json`, 'wx', (err, fileDescriptor) => {
    if (!err && fileDescriptor) {
      const stringData = JSON.stringify(data);
      fs.writeFile(fileDescriptor, stringData, (err2) => {
        if (!err2) {
          fs.close(fileDescriptor, (err3) => {
            if (err3) {
              callback('File Created');
            } else {
              callback('Error closing the new file');
            }
          });
        } else {
          callback('Error writting new file');
        }
      });
    } else {
      callback('There was a error, file name is already exist!');
    }
  });
};

and:

handle._users.post = (requestProperties, callback) => {
  const firstName =
    typeof requestProperties.body.firstName === 'string' &&
    requestProperties.body.firstName.trim().length > 0
      ? requestProperties.body.firstName
      : null;
  const lastName =
    typeof requestProperties.body.lastName === 'string' &&
    requestProperties.body.lastName.trim().length > 0
      ? requestProperties.body.lastName
      : null;
  const phone =
    typeof requestProperties.body.phone === 'string' &&
    requestProperties.body.phone.trim().length === 11
      ? requestProperties.body.phone
      : null;
  const password =
    typeof requestProperties.body.password === 'string' &&
    requestProperties.body.password.trim().length > 0
      ? requestProperties.body.password
      : null;
  const tosAgree =
    typeof requestProperties.body.tosAgree === 'boolean' && requestProperties.body.tosAgree
      ? requestProperties.body.tosAgree
      : null;
  if (firstName && lastName && phone && password && tosAgree) {
    lib.read('users', phone, (err) => {
      if (err) {
        const userObject = {
          firstName,
          lastName,
          phone,
          password: hash(password),
          tosAgree,
        };
        lib.create('users', phone, userObject, (err2) => {
          console.log(err2);
          if (!err2) {
            callback(200, {
              Success: 'User created',
            });
          } else {
            callback(500, {
              Error: 'Could not create user',
            });
          }
        });
      } else {
        callback(500, {
          Error: 'There was a problem, may user already exist',
        });
      }
    });
  } else {
    callback(400, {
      Error: 'There is a probelm with your request',
    });
  }
};

Problem is when the file is created then result is showing "callback(500, {Error: 'Could not create user'});" But when file already exist can't create new one then result is showing callback(500, {Error: 'User created'});

But I check multiple time and I see the logic was right but when file is successfully created then it's showing error message. Can anyone help me to fix it.

  • Callbacks are supposed to pass two arguments. The first for error and the second for data. But, in the `lib.create`, you are always passing one argument even if the action is successful. `callback(null, 'File Created')` will solve your problem. Using that, the caller can distinguish an error state from a successful one. By the way, `if (err3) {callback('File Created')` smells bad. If there is an error why did you notice the file was created? – Raeisi Aug 27 '23 at 04:56

0 Answers0