如何将新对象添加到我包含在我的配置文件模型中的空数组中(mongodb/mongoose)

网络。

这是我的个人资料模型,

const ProfileSchema = new mongoose.Schema({
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
  },
  company: String,
  website: String,
  location: String,
  status: {
    type: String,
    required: true,
  },
  skills: {
    type: [String],
    required: true,
  },
  bio: String,
  githubusername: String,
  experience: [
    {
      title: {
        type: String,
        required: true,
      },
      company: {
        type: String,
        required: true,
      },
      location: String,
      from: {
        type: Date,
        required: true,
      },
      to: Date,
      current: {
        type: Boolean,
        default: false,
      },
      description: String,
    },
  ],
  education: [
    {
      school: {
        type: String,
        required: true,
      },
      degree: {
        type: String,
        required: true,
      },
      fieldofstudy: {
        type: String,
        required: true,
      },
      from: {
        type: Date,
        required: true,
      },
      to: Date,
      current: {
        type: Boolean,
        default: false,
      },
      description: String,
    },
  ],
  social: {
    youtube: {
      type: String,
    },
    twitter: {
      type: String,
    },
    facebook: {
      type: String,
    },
    linkedin: {
      type: String,
    },
    instagram: {
      type: String,
    },
  },
  date: {
    type: Date,
    default: Date.now,
  },
  posts: [],
});

这就是我添加新帖子的方式,

router.post(
  "/",
  [auth, [check("text", "Text is required").not().isEmpty()]],
  async (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }

    try {
      const user = await User.findById(req.user.id).select("-password");

      let profile = await Profile.findOne({ user: req.user.id });
      if (profile) {

        const newPost = new Post({
          text: req.body.text,
          name: user.name,
          avatar: user.avatar,
          user: req.user.id,
        });

        const post = await newPost.save();
        profile.posts.unshift(post);
        res.json(post);
      }
    } catch (err) {
      console.error(err.message);
      res.status(500).json({ errors: [{ msg: "Server Error" }] });
    }
  }
);

大多数这些工作没有任何问题,我可以添加一个新帖子而不会出现任何错误,但是该帖子没有添加到我的个人资料中的帖子数组中。我想要的最终结果是保留用户帖子的记录,以便我可以在他的个人资料中单独显示它们。为什么这不起作用?请帮我!我是 Web 开发的新手,对此我感到很困惑。提前致谢。

萨尔曼

我对您的问题的理解是您想将帖子添加到个人资料架构中。

const post = await newPost.save();
profile.posts.unshift(post);
await profile.save();
res.json(post);

你也需要调用 save ,你错过了。现在帖子将被推送到帖子字段中的个人资料集合。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用for循环将AutoMapper配置文件添加到我的映射器配置中?

Typescript .push 函数将空对象添加到我的数组中

如何将新对象添加到现有实体,将数据放入数据中并让EF将其添加到我的数据库中?

如何将appsettings.json文件添加到我的Azure Function 3.0配置中?

如何将新的数据行添加到我的 Derby 数据库中?

如何将外部数组添加到我的 Rx Observable 中?

如何将另一个文件添加到我的请求中?

如何将Changelog或NEWS文件添加到我的R包中?

如何将 css 文件添加到我的 TinyMCE 模板中?

你调用的对象是空的。当我将项目添加到我的数组列表中时

如何将构建期间生成的文件添加到我的包含目录?

如何将数组添加到我的 php 变量中,循环不适用于数组

如何将输出到我的表格中的文本添加到一个数组中?

我如何将 AWS env varaible 添加到我的包 json 中

我如何将物品添加到我的LI物品中

VSTS 如何将新的问题工作项添加到我的待办事项列表中?

将配置文件添加到我的JavaScript项目

将jQuery添加到我的Javascript文件中

如何将通过树枝从对象生成的行添加到我的数据表中?

如何将Firestore中的时间戳添加到我的变量中

如何将 ggplot 中的图例添加到我的图表中

xcode 5:如何将UUID添加到xcode管理的配置文件中?

EF Core 5如何停止将ValueGeneratedOnAdd添加到我的所有模型中

如何将字符串添加到我的文本文件中的第 13 行

也门,咕unt声-如何将横幅广告添加到我的生成文件中

如何将“容器”包添加到我的.cabal文件中(在编译时不会被堆栈覆盖)?

如何将唯一ID添加到我的(python)Splinter屏幕快照文件名中?

如何将“ rails”的可执行文件添加到我的容器的$ PATH中?

如何将ohloh.net小部件添加到我的github项目readme.md文件中?