【问题标题】:error: users.find is not a function but acttualy it is错误:users.find 不是一个函数,但实际上它是
【发布时间】:2020-12-21 20:55:38
【问题描述】:

我正在尝试使用NODE.JSexpressaxios 为项目创建我的第一个 API。

当我尝试从终端使用node app.js(app.js 是服务器文件)时,一切都很好,所以我使用node cli.js 启动 CLI,我在其中输入日期,如电子邮件、用户名、密码 ecc .. 当服务器接受用户的请求时,它会抛出这个错误:

这是服务器代码(app.js)的一部分:

var users = require('./users.json');

{console.log(req.body);
if(req.body.new_password === req.body.new_password_confirm && req.body.new_email === req.body.new_email_confirm ){

    let user = users.find((item )=>{ //<---- the method "find" who generate error
        return item.username === req.body.new_username || item.email === req.body.new_email;
    });
    
    if(!!user){
        res.send("email o username già in uso");
    }else 
    if(!!user == false){
        res.status(200).send("utente aggiunto");
        let user_json = JSON.stringify(req.body);
        fs.writeFileSync('users.json', user_json);
    }
}else
res.status(200).send("email o password errati");
});`

这是客户端代码(cli.js)的一部分:

 axios
  .post("http://localhost:8080/", answers)
  .then((response) => {
    if (response.data) {
      var registration_data = [
        {
          type: "input",
          name: "new_email",
          message: "email:",
        },
        {
          type: "input",
          name: "new_email_confirm",
          message: "confirm email:",
        },
        {
          type: "input",
          name: "new_username",
          message: "username:",
        },
        {
          type: "password",
          name: "new_password",
          message: "password:",
          mask: "*",
        },
        {
          type: "password",
          name: "new_password_confirm",
          message: "confirm password:",
          mask: "*",
        },
      ];

      inquirer.prompt(registration_data).then(async (answers) => {
        console.log("\nTrying Validation... ");

        axios
          .post("http://localhost:8080/new_user", answers)
          .then((response) => {
            console.log(response.data);
          });
      });
    } else if (!response.data) {
      console.log("TODO: LOGIN");
    }
  })
  .catch((response) => {
    console.error(response);
  });

IDK 为什么错误说 .find 实际上不是函数。

【问题讨论】:

  • 上线之前你访问users,为什么不记录一下,看看里面是否包含你认为包含的内容
  • 假设users是JSON,它不能包含任何函数。
  • .find 是一个有效的函数,如果 JSON 包含一个数组
  • No JSON 如果没有被解析成有效的 JS 对象,它将无法工作..

标签: javascript node.js api express axios


【解决方案1】:

问题是你没有在 JS 中解析 JSON。

尝试在开头添加:

const users = require('./users.json');
const jsUsers = JSON.parse(users);

然后在您的 sn-p 中使用 jsUsers 而不是用户。

这应该可以帮助您使用find

再见!

【讨论】:

  • 我添加了这两个代码行,但唯一的区别是输出错误:SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (&lt;anonymous&gt;) at Object.&lt;anonymous&gt;
  • 新输出是什么?
【解决方案2】:

好的,经过一番尝试,我已经解决了。

我是个白痴,对不起。

看看 user.json 里面有什么:

{
    "new_email": "",
    "new_email_confirm": "",
    "new_username": "",
    "new_password": "",
    "new_password_confirm": ""
  }

是的,完全没有方括号,user.json 是一个对象而不是数组。

现在 json 文件看起来像这样:

[
  {
    "new_email": "",
    "new_email_confirm": "",
    "new_username": "",
    "new_password": "",
    "new_password_confirm": ""
  }
]

所以,下次我会记住,.find 只能接受数组而不是对象。 谢谢大家的支持

【讨论】:

    猜你喜欢
    • 2021-07-18
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 2016-04-05
    • 2021-08-18
    相关资源
    最近更新 更多