使用猫鼬从 Atlas Mongo 获取数据到 angular 应用程序

Data fetching from Atlas Mongo with mongoose to angular app

我在我的注册页面中添加了一个新输入,即名称字段,并且还更改了架构中的设置。现在我如何获取该名称以在我的 post-list 组件和 post-create 组件中使用?

这是创建用户的语法。现在我想在 post 标题处使用这个名称。

{

  _id: 5eb54bb209a6c405e5fb5b97,

  name: 'chokko',

  email: 'chokko@test',

  password: '********************************'

}

createUser.js

exports.createUser = (req, res, next) => {
  bcrypt.hash(req.body.password, 10).then((hash) => {
    const user = new User({
      name: req.body.name,
      email: req.body.email,
      password: hash,
    });
    user
      .save()
      .then((result) => {
        res.status(201).json({ message: "User created!", result: result });
      })
      .catch((err) => {
        res
          .status(500)
          .json({ message: "Invalid authentication credentials!" });
      });
  });
};

您可以使用 Model.findById() 获取特定文档,其中包含 'projection' 到 select 字段(在本例中为 'name' 字段)。

User.findById(id,'-id name')

此外,您必须显式删除 _id ('-id'),除非您希望它与 selected 字段一起返回。