【问题标题】:Mongoose saves empty object but object is not empty - Nodejs猫鼬保存空对象但对象不为空 - Nodejs
【发布时间】:2021-06-26 23:33:49
【问题描述】:

我正在尝试解析一个 CSV 文件(可以包含多个条目)并将数据直接存储到我的猫鼬数据库中。我的代码似乎可以正确解析数据,因为当我调试和快速查看“实体”和“临时”变量时,我会看到来自 CSV 的数据条目。但是,当我将对象存储在数据库中时,我看到只有一个自动生成的 ID 的空对象。

testCSVfile.csv

author, ownerName, authorID, contactInfo, published, summary
jon doe, bob, 321, jondoe@gmail, 03/17, testing testing testing

这是我的 uploadCSV.js 文件

const mongoose = require("mongoose");
const passport = require("passport");
const csvtojson = require("csvtojson");
const router = require("express").Router();
const bookModel= mongoose.model("Book");
const async = require('async');

csvtojson()
        .fromFile("testCSVfile.csv")
        .then(csvData => {
            async.eachSeries(csvData,(data,callback) => {
                  let entity = {
                    author: data.author,
                    ownerName: data.ownerName,
                    authorID: data.authorID,
                    contactInfo: data.contactInfo,
                    published: data.published,
                    summary: data.summary
                    };


                    bookModel.create({entity}, function(err)
                    {
                        if(err) return callback(err);
                        return callback(null);    
                    })
               },
                (err) => {
                     if(err) console.log(err); 
                     console.log("books are successfully imported!!!");
                     console.log(entity);
                });            
});

终端输出

[
[0]   {
[0]     author: 'jon doe',
[0]     ownerName: 'bob',
[0]     authorID: '123',
[0]     contactInfo: 'jondoe@gmail',
[0]     published: '03/17',
[0]     summary: 'testing testing testing',
[0]   }

到目前为止,CSV 文件是本地文件,但最终,它将来自从前端上传的用户。即使实体变量具有正确的数据,它也没有正确保存。如果有人能给我一些建议,将不胜感激!

【问题讨论】:

  • 我看不到projectModel变量声明!,你应该使用bookModel.create
  • @TusharGupta-curioustushar 嗨,好收获!那是我的一个错字。在代码中其实是正确的。
  • 您是否遇到任何错误? entity 的值是多少,你能在 eachSeries 里面打印吗
  • @TusharGupta-curioustushar 我在打印实体时没有收到任何错误 - 上面显示的终端输出 ^ 是打印输出的内容。
  • 你能展示一下data变量的样子吗?

标签: node.js database mongodb csv mongoose


【解决方案1】:

您尚未为您的 mongoose 模型定义架构,因此当架构为空时,它将允许您在数据库中插入任何内容。

https://mongoosejs.com/docs/guide.html#strict

strict 选项(默认启用)确保传递给模型构造函数但未在我们的架构中指定的值不会保存到数据库中。

为模型创建猫鼬模式

https://mongoosejs.com/docs/guide.html#definition

const bookSchema = new mongoose.Schema({
    author: String,
    ownerName: String,
    // ........ rest of your model properties
});
const bookModel= mongoose.model("Book", bookSchema);

改变

bookModel.create({entity}, function(err)

bookModel.create(entity, function(err)

【讨论】:

  • 不是行“const bookModel= mongoose.model("Book");"从模式创建模型?我认为我的主要问题是我的对象不是空的,但保存的对象是。
  • @CharlieMasterson 请阅读mongoosejs.com/docs/guide.html#strict 如果未定义模式,模型将不会在严格模式下插入任何内容。
  • 我添加了代码,它仍然将空对象存储到数据库中
  • @CharlieMasterson 更改 bookModel.create(entity, function(err)
  • @CharlieMasterson 很高兴它有帮助:)
猜你喜欢
  • 1970-01-01
  • 2017-04-27
  • 2014-07-26
  • 2018-07-12
  • 2016-06-18
  • 2021-01-30
  • 2016-01-03
  • 2018-02-28
  • 2014-02-10
相关资源
最近更新 更多