arrays - NodeJS & Mongoose : Add element to existing empty array

标签 arrays json node.js mongoose updatemodel

我正在构建一个 NodeJS 应用程序,用于根据 MSQL 数据库中的数据创建报告。所有应用程序相关数据都使用 Mongoose 存储在 MongoDB 中。我的 Mongoose 模型包含一个空数组,然后用户通过 Rest-API 填充该数组。 向数组添加新元素时出现错误。我已经尝试过 model.array.push(object); model.save() 和 findByIdAndUpdate(...)。找到我的代码,包括以下两种不同的尝试:

Mongoose 架构

var sqlSchema = mongoose.Schema({ // Schema to store information about SQL-procedures
    'name': String,
    'inputs': [
        {
            'name': String,
            'type': String,
            'value': String,
            'adjustable': Boolean
        }
    ]
});

REST API

我的应用程序通过 POST 接受“输入”数组的新元素:

var mongoose = require('mongoose'),
    SqlProcedure = mongoose.model('SqlProcedure');

// ...

router.post('/sql/:id/inputs', function(req, res) {
    SqlProcedure.findById(req.params.id, function(err, sql) {
        if(err) {
            res.send(msg.error('error retrieving sql-procedure', err));
        } else {
            if(sql.inputs.length > 0) {     // catch empty array
                for(var key in sql.inputs) {
                    if(sql.inputs[key].name == req.body.name) {
                        return res.send(msg.error('input already in inputs', err));
                    }
                }
            }

            var data = req.body;

            var input = {
                name: data.name,
                type: data.type,
                value: data.value,
                adjustable: data.adjustable
            };

            // attempt one or two
        }
    });
});

尝试一:

            sql.inputs.push(input); // EXCEPTION 1

            sql.save(function(err) {
                // fancy errorhandling
                return res.send(msg.ok(sql));
            });

尝试二:

            SqlProcedure.findByIdAndUpdate(req.params.id,
                {$push: {inputs: input}}, // EXCEPTION 2
                {safe: true, upsert: true},
                function(err, sql) {
                    // fancy errorhandling
                    res.send(msg.ok('input added to sql-procedure' + req.params.id));
                }
            );

异常

尝试一:

throw new CastError('string', value, this.path);
  ^
Error
    at MongooseError.CastError (\node_modules\mongoose\lib\error\cast.js:18:16)
    at SchemaString.cast (\node_modules\mongoose\lib\schema\string.js:434:9)
    at Array.MongooseArray.mixin._cast (\node_modules\mongoose\lib\types\array.js:124:32)
    at Array.MongooseArray.mixin._mapCast (\node_modules\mongoose\lib\types\array.js:295:17)
    at Object.map (native)
    at Array.MongooseArray.mixin.push (\node_modules\mongoose\lib\types\array.js:308:25)
    at Query.<anonymous> (\app\api\sql_procedure.js:69:28)
    at \node_modules\mongoose\node_modules\kareem\index.js:177:19
    at \node_modules\mongoose\node_modules\kareem\index.js:109:16
    at doNTCallback0 (node.js:417:9)
    at process._tickCallback (node.js:346:13)

尝试二:

"stack": "Error
at MongooseError.CastError (\\node_modules\\mongoose\\lib\\error\\cast.js:18:16)
at SchemaArray.cast (\\node_modules\\mongoose\\lib\\schema\\array.js:156:15)
at SchemaArray.cast (\\node_modules\\mongoose\\lib\\schema\\array.js:167:17)
at Query._castUpdateVal (\\node_modules\\mongoose\\lib\\query.js:2384:22)
at Query._walkUpdatePath (\\node_modules\\mongoose\\lib\\query.js:2298:27)
at Query._castUpdate (\\node_modules\\mongoose\\lib\\query.js:2227:23)
at castDoc (\\node_modules\\mongoose\\lib\\query.js:2430:18)
at Query._findAndModify (\\node_modules\\mongoose\\lib\\query.js:1752:17)
at Query._findOneAndUpdate (\\node_modules\\mongoose\\lib\\query.js:1620:8)
at \\ITZReport\\node_modules\\mongoose\\node_modules\\kareem\\index.js:156:8
at \\node_modules\\mongoose\\node_modules\\kareem\\index.js:18:7
at doNTCallback0 (node.js:417:9)\n    at process._tickCallback (node.js:346:13)",
    "message": "Cast to undefined failed for value \"[object Object]\" at path \"inputs\"",
    "name": "CastError",
    "value": [
        {
            "adjustable": "true",
            "value": "Harry Potter",
            "type": "String",
            "name": "salesman"
        }
    ],
    "path": "inputs"

要插入的数据

{ name: 'salesman',
  type: 'String',
  value: 'Harry Potter',
  adjustable: 'true' }

我是 NodeJS 和 mongoose 的新手,并尝试自己解决这个问题很多小时。如果有人能帮助我,那就太好了!

提前致谢, DJ2蜜蜂

更新

我认为我应该澄清用户与 REST-API 交互的过程:

  1. 用户通过传递值来创建新记录 名称。此时,name 已设置,并且 inputs 数组已设置 是空的。
  2. 在下一步中,用户将新记录添加到 inputs-数组一一对应。 名称保持原样,并且仅 新的输入被添加到数组中。
  3. 用户应该能够编辑或删除条目 输入-数组。

adjustable 的数据类型更改为 String 并没有做出任何更改。我还尝试了对属性进行硬编码,而不是通过 HTTP-POST 传递它们 - 仍然是相同的异常。

最佳答案

经过几个小时的网络搜索并测试代码中最奇怪的事情后,我找到了解决方案: 您不能在 Mongoose 模式中将字段命名为“类型”。就是这样。

正确的代码如下所示:

Mongoose 架构

var sqlSchema = mongoose.Schema({
    'name': String,
    'inputs': {
        'name': String,
        'datatype': String, // you can't name this field 'type'
        'value': String,
        'adjustable': Boolean
    }
});

REST API

router.post('/sql/:id/inputs', function(req, res) {
    SqlProcedure.findById(req.params.id, function(err, sql) {
        if(err) {
            res.send(msg.error('error retrieving sql-procedure', err));
        } else {
            if(!sql) {
                return res.send(msg.error('no sql-procedure found with id '
                    + req.params.id, null));
            }
            // check for duplicates

            var data = req.body;

            var input = {
                'name': data.name,
                'datatype': data.type,
                'value': data.value,
                'adjustable': data.adjustable
            };

            SqlProcedure.findByIdAndUpdate(req.params.id,
                {$push: {inputs: input}},
                {safe: true, upsert: true},
                function(err, sql) {
                    // fancy error handling
                }
            );
        }
    });
});

关于arrays - NodeJS & Mongoose : Add element to existing empty array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33760550/

相关文章:

python - 将 NumPy 数组转换为集合花费的时间太长

arrays - Swift:如何在 TableView 内的 CollectionView 中显示解析的 JSON 数据?

javascript - 尝试运行 Electron 应用程序时找不到模块应用程序

python - os.listdir 使用数组的输入

javascript - Angular 对象如何获取另一个对象的值

java - 在ArrayList中添加默认值

json - 使用customResource将RDS层注册到opsworks堆栈

json - jq 过滤子对象值

javascript - Node js for 循环定时器

node.js - ubuntu npm安装错误