无法在MongoDB中插入记录

new9mtju  于 2023-10-16  发布在  Go
关注(0)|答案(3)|浏览(132)

有人能帮助我理解为什么我不能在mongoDB中插入任何东西吗?
这是我的邮寄路线

router.post('/', function(req, res, next) {

  console.log("i am posting data!");

  var MongoClient = mongodb.MongoClient;
  var url = 'mongodb://localhost:27017/blog';

  MongoClient.connect(url, function(err, db) {

    if (err) {

      console.log('Error:', err);

    } else {

      console.log('connected to db');

      var collection = db.collection('blogPost');

      var blogPost = {

        title: req.body.title,
        status: req.body.status,
        category: req.body.category,
        tags: req.body.tags,
        content: req.body.content,
        createDate: new Date()

      }

      console.log("posting data:", blogPost);

      collection.insert(blogPost, function(err, res) {

        if (err) {

          console.log('Error on Insert:', err);
        } else {

          console.log("insert successful");

        }

        db.close();
        res.render('index', {
          title: 'Welcome!!!',
          pageName: 'index'
        });

      });

    }

    db.close();

  });

});

下面是我在控制台日志中看到的内容。

GET /js/bootstrap-multiselect.js 200 16.638 ms - 66344
i am posting data!
connected to db
posting data: { title: 'test',
  status: 'A',
  category: 'excerpt',
  tags: [ 'life', 'teaching' ],
  content: 'Test from app',
  createDate: Sun Sep 04 2016 23:46:18 GMT-0600 (MDT) }
insert successful
Sun Sep 04 2016 23:46:18 GMT-0600 (MDT): Node server stopped.
/Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/utils.js:98
    process.nextTick(function() { throw err; });
                                  ^

TypeError: res.render is not a function
    at /Users/bberrelez/Projects/Personal/WebApp/illcomplacent/routes/index.js:94:13
    at /Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/collection.js:523:5
    at /Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/collection.js:701:5
    at handleCallback (/Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/utils.js:96:12)
    at executeCommands (/Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/bulk/ordered.js:405:12)
    at resultHandler (/Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/bulk/ordered.js:433:5)
    at /Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:436:18
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickCallback (node.js:349:13)
Brians-MacBook-Pro:illcomplacent bberrelez$

问题是,它甚至没有插入MongoDB。我可以从命令行很好地插入数据,但这不是我需要做的。我需要从应用程序中插入此数据。

sxissh06

sxissh061#

您已经使用了相同的变量名res两次。当你到达res.render时,res是本地作用域到你的回调,它引用的是你插入的结果,而不是你的响应对象。
要解决这个问题,你可以简单地重命名你的插入结果。尝试更改:

collection.insert(blogPost, function(err, res) {

collection.insert(blogPost, function(err, result) {

这并不能解释为什么没有插入数据,但看起来数据应该在那里。你确定你正在寻找正确的数据库(博客)和集合(blogPost)?

fjnneemd

fjnneemd2#

你已经给了res作为insert callback function中的参数,这就是为什么你得到这个。
local rest没有render功能。
希望这对你有用。

router.post('/', function(req, res, next) {

  console.log("i am posting data!");

  var MongoClient = mongodb.MongoClient;
  var url = 'mongodb://localhost:27017/blog';

  MongoClient.connect(url, function(err, db) {

    if (err) {

      console.log('Error:', err);

    } else {

      console.log('connected to db');

      var collection = db.collection('blogPost');

      var blogPost = {

        title: req.body.title,
        status: req.body.status,
        category: req.body.category,
        tags: req.body.tags,
        content: req.body.content,
        createDate: new Date()

      }

      console.log("posting data:", blogPost);

      collection.insert(blogPost, function(err, result) {// change `res` with any other vaiable

        if (err) {

          console.log('Error on Insert:', err);
        } else {

          console.log("insert successful");

        }

        db.close();
        res.render('index', {
          title: 'Welcome!!!',
          pageName: 'index'
        });

      });

    }

    db.close();
  });
});
wn9m85ua

wn9m85ua3#

我用Django做了类似的事情,驱动程序将我的集合名称转换为小写。Node.js可能也在做同样的事情。尝试检查Mongo shell,看看您的代码是否创建了一个blogpost集合,并将文档插入到那里而不是blogPost中。

相关问题