Arrays NodeJS&;Mongoose:将元素添加到现有空数组
我正在构建一个NodeJS应用程序,用于根据MSQL数据库中的数据创建报告。所有与应用程序相关的数据都使用Mongoose存储在MongoDB中。我的mongoose模型包含一个空数组,然后由用户通过RESTAPI填充。 向数组中添加新元素时出错。我已经用Arrays NodeJS&;Mongoose:将元素添加到现有空数组,arrays,json,node.js,mongoose,updatemodel,Arrays,Json,Node.js,Mongoose,Updatemodel,我正在构建一个NodeJS应用程序,用于根据MSQL数据库中的数据创建报告。所有与应用程序相关的数据都使用Mongoose存储在MongoDB中。我的mongoose模型包含一个空数组,然后由用户通过RESTAPI填充。 向数组中添加新元素时出错。我已经用model.array.push(object)试过了;model.save()。查找我的代码,包括以下两种不同的尝试: 猫鼬模式 RESTAPI 我的应用程序通过POST接受“输入”数组的新元素: var mongoose = require
model.array.push(object)试过了;model.save()。查找我的代码,包括以下两种不同的尝试:
猫鼬模式
RESTAPI
我的应用程序通过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));
});
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)
尝试二:
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));
}
);
"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"
例外情况
尝试一:
sql.inputs.push(input); // EXCEPTION 1
sql.save(function(err) {
// fancy errorhandling
return res.send(msg.ok(sql));
});
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)
要插入的数据
我对NodeJS和mongoose是个新手,我花了好几个小时试图自己解决这个问题。如果有人能帮助我那就太好了
提前感谢,,
dj2bee
更新
我想我应该澄清用户与REST-API交互的过程:
用户通过传递
名称
。此时设置名称
,并且输入
-数组
是空的
在下一步中,用户将新记录添加到
输入
-逐个排列。名称保持原样,仅
新的输入将添加到数组中
用户应该能够编辑或删除列表中的条目
输入
-阵列
将adjustable
的数据类型更改为String没有进行任何更改。我还尝试了硬编码属性,而不是通过HTTP-POST传递它们-仍然是相同的例外。您应该将数据作为
{ name: 'salesman',
inputs: [{name: 'salesman',
type: 'String',
value: 'Harry Potter',
adjustable: true}]}
i、 e.true,不带引号和作为数组的输入
或者在模式中使可调整为字符串,并在模型定义中删除作为数组的输入
var sqlSchema = mongoose.Schema({ // Schema to store information about SQL-procedures
'name': String,
'type': String,
'value': String,
'adjustable': String
});
经过数小时的网络搜索和代码测试,我找到了解决方案:
不能在mongoose模式中命名字段“type”。就是这样
正确的代码如下所示:
猫鼬模式
RESTAPI
我硬编码'adjustable'为true,但仍然得到相同的错误:在路径\“inputs\”处的值\“[object object]”转换为未定义失败。我忽略了模型将所有输入都作为数组。请参阅更正的数据在将记录添加到数组之前设置了名称
:请参阅上面的“我的更新”。我不知道还有什么办法可以解决这个问题。这有点令人沮丧:D
var sqlSchema = mongoose.Schema({
'name': String,
'inputs': {
'name': String,
'datatype': String, // you can't name this field 'type'
'value': String,
'adjustable': Boolean
}
});
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
}
);
}
});
});