最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何通过JSON对象将数据保存到mongodb数据库?

运维笔记admin20浏览0评论

如何通过JSON对象将数据保存到mongodb数据库?

如何通过JSON对象将数据保存到mongodb数据库?

我想将数据保存到数据库中。我曾经使用数据库检查URL。但是我做不到。它显示此消息:

DeprecationWarning:不建议使用当前的URL字符串解析器,在将来的版本中删除。要使用新的解析器,请传递选项{useNewUrlParser:true}到MongoClient.connect。侦听端口3000{_id:5d9775340007cc3c8cb34caf} {_id:5d9775340007cc3c8cb34caf,__v:0}错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头

这是我的路由器文件代码:

router.post("/timeTableRegistration", function(req, res) {
  const newTimeTable = new classTimeTable({
    object: req.body
  });
  console.log(newTimeTable);
  newTimeTable
    .save()
    .then(result => {
      console.log(result);
      res.json({ state: true, msg: "Data Inserted Successfully..!" });
    })
    .catch(error => {
      console.log(error);
      res.json({ state: false, msg: "Data Inserting Unsuccessfull..!" });
    });
  res.send("Success");
});

这是我的模型文件:

const mongoose = require("mongoose");
const Schema = mongoose.schema;

const classTimeTableSchema = mongoose.Schema({
  className: { type: String, require: true },

  monday: {
    1: { type: String, require: true },
    2: { type: String, require: true }
  },
  tuesday: {
    1: { type: String, require: true },
    3: { type: String, require: true }
  }
});

const classTimeTable = (module.exports = mongoose.model(
  "classTimeTable",
  classTimeTableSchema
));
回答如下:

您只需要在连接到mongodb的位置添加选项useNewUrlParser:true即可。它还在提供的错误中明确指出了这一点。标头已经发送的通知是您之前已经使用过输出,因此很可能只是因为您没有正确捕获错误。

发布评论

评论列表(0)

  1. 暂无评论