如何使用猫鼬获得身份证?

发布于 2025-02-13 21:16:48 字数 1478 浏览 0 评论 0原文

因此,我遵循本YouTube教程以学习MongoDB和Mangoose的基础知识,我设法设置了所有内容,直到我必须显示ID为止。我已经尝试关注视频,但出现“未定义”而不是实际的ID。我已经尝试在Console.log中打印,但是ID仅在我使用时才打印:console.log(article._id);而不是控制台。

有任何解决这个问题的想法吗?以下是我的代码。

这是我的文章。js文件:

const express = require('express');
const Article = require('./../models/article')
const router =  express.Router();

router.get('/new', (req, res) => {
  res.render('articles/new', { article: new Article() })
});

router.get('/:id', (req, res) => {
  res.send(req.params.id);
});

router.post('/', (req, res) => {
  let article = new Article({
    title: req.body.title,
    description: req.body.description,
    markdown: req.body.markdown
  });   
  try {
    console.log("Try Step 1");
    console.log(article._id);
    article = article.save()
    console.log("Try Step 2");
    res.redirect(`/articles/${article.id}`);
    console.log("Try Step 3");
  } catch (e) {
    console.log("Error");
    res.render('articles/new', { article: article });
  }
});

module.exports = router

这是我的文章.js文件,它为文章创建架构:

const mongoose = require('mongoose');

const articleSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true
  },
  description: {
    type: String
  },
  markdown: {
    type: String,
    required: true
  },
  createdAt: {
    type: Date, 
    default: Date.now
  }
}); 

module.exports = mongoose.model('Article', articleSchema);   

如果需要,我可以添加任何信息。谢谢你的宝贵时间。

So i'm following this youtube tutorial to learn the basics of mongodb and mangoose, I've managed to setup everything until I have to display an id. I've tried following the video but "undefined" appears instead of the actual id. I've tried printing in console.log but the id only prints when i use: console.log(article._id); and not console.log(article.id);

Any ideas for fixing this? Below is my code.

Here is my articles.js file :

const express = require('express');
const Article = require('./../models/article')
const router =  express.Router();

router.get('/new', (req, res) => {
  res.render('articles/new', { article: new Article() })
});

router.get('/:id', (req, res) => {
  res.send(req.params.id);
});

router.post('/', (req, res) => {
  let article = new Article({
    title: req.body.title,
    description: req.body.description,
    markdown: req.body.markdown
  });   
  try {
    console.log("Try Step 1");
    console.log(article._id);
    article = article.save()
    console.log("Try Step 2");
    res.redirect(`/articles/${article.id}`);
    console.log("Try Step 3");
  } catch (e) {
    console.log("Error");
    res.render('articles/new', { article: article });
  }
});

module.exports = router

Here is my article.js file which creates the schema for article:

const mongoose = require('mongoose');

const articleSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true
  },
  description: {
    type: String
  },
  markdown: {
    type: String,
    required: true
  },
  createdAt: {
    type: Date, 
    default: Date.now
  }
}); 

module.exports = mongoose.model('Article', articleSchema);   

I can add any info if needed. Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强辩 2025-02-20 21:16:48

默认情况下, mongoose 在模式中添加了 _id 属性。因此,当您插入一个新文档时, mongoose 会自动添加 _id 对象类型到文档。

不幸的是,进入 mongodb _id 属性是不可变的,您无法重命名 _id 属性。
有关 _id - “ nofollow noreferrer”>链接

_id 可以覆盖属性,即您可以将 _id 的类型从对象类型更改为您自己的类型。但是,然后您必须手动将值添加到 _id 属性中。

例如,

const schema = new Schema({_id: Number});
const model = new Model('Test', schema);

// Create new document
const doc = new Model();
await doc.save(); // Throws "document must have an _id before saving"

// Add id to document
doc._id = 1;
await doc.save(); // works

请参阅此链接以了解有关Mongoose ID的更多信息 - link

By default mongoose adds an _id property to the schema. So, when you insert a new document, mongoose automatically adds the _id of Object Type to the document.

Coming to MongoDB, Unfortunately, _id property is immutable, you can't rename the _id property.
Refer this for know more about behavior of _id - link

_id property can be overwritten i.e. you can change the type of _id from Object Type to your own type. But, then you manually have to add value to _id property.

For Example

const schema = new Schema({_id: Number});
const model = new Model('Test', schema);

// Create new document
const doc = new Model();
await doc.save(); // Throws "document must have an _id before saving"

// Add id to document
doc._id = 1;
await doc.save(); // works

Please refer this link for know more about Mongoose Ids - link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文