gpt4 book ai didi

node.js - 无法将子文档推送到 Mongoose 中的文档数组

转载 作者:太空宇宙 更新时间:2023-11-04 03:08:39 25 4
gpt4 key购买 nike

我正在尝试将一堆子文档推送到文档的数组中。我做错了什么?

下面提供了错误和代码。

错误:

21:28:49 web.1  | { [CastError: Cast to undefined failed for value "{ url: 'http://www.bbc.co.uk',
21:28:49 web.1 | _id: 55a17c812424c54413de3332,
21:28:49 web.1 | entities: [],
21:28:49 web.1 | terms: [] },{ url: 'http://www.google.com',
21:28:49 web.1 | _id: 55a17c812424c54413de3333,
21:28:49 web.1 | entities: [],
21:28:49 web.1 | terms: [] }" at path "documents"]
21:28:49 web.1 | stack: 'Error\n at MongooseError.CastError (/Users/martynas/Desktop/sandbox/osint-api/node_modules/mongoose/lib/error/cast.js:18:16)\n at SchemaArray.cast (/Users/martynas/Desktop/sandbox/osint-api/node_modules/mongoose/lib/schema/array.js:157:15)\n at Query._castUpdateVal (/Users/martynas/Desktop/sandbox/osint-api/node_modules/mongoose/lib/query.js:2270:22)\n at Query._walkUpdatePath (/Users/martynas/Desktop/sandbox/osint-api/node_modules/mongoose/lib/query.js:2240:25)\n at Query._castUpdate (/Users/martynas/Desktop/sandbox/osint-api/node_modules/mongoose/lib/query.js:2118:23)\n at Query.update (/Users/martynas/Desktop/sandbox/osint-api/node_modules/mongoose/lib/query.js:1959:22)\n at Function.update (/Users/martynas/Desktop/sandbox/osint-api/node_modules/mongoose/lib/model.js:1734:13)\n at exports.populateCase (/Users/martynas/Desktop/sandbox/osint-api/app/controllers/case.js:69:10)\n at Layer.handle [as handle_request] (/Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/layer.js:82:5)\n at next (/Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/route.js:100:13)\n at Route.dispatch (/Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/route.js:81:3)\n at Layer.handle [as handle_request] (/Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/layer.js:82:5)\n at /Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/index.js:234:24\n at param (/Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/index.js:331:14)\n at param (/Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/index.js:347:14)\n at Function.proto.process_params (/Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/index.js:391:3)',
21:28:49 web.1 | message: 'Cast to undefined failed for value "{ url: \'http://www.bbc.co.uk\',\n _id: 55a17c812424c54413de3332,\n entities: [],\n terms: [] },{ url: \'http://www.google.com\',\n _id: 55a17c812424c54413de3333,\n entities: [],\n terms: [] }" at path "documents"',
21:28:49 web.1 | name: 'CastError',
21:28:49 web.1 | kind: undefined,
21:28:49 web.1 | value: [{"url":"http://www.bbc.co.uk","_id":"55a17c812424c54413de3332","entities":[],"terms":[]},{"url":"http://www.google.com","_id":"55a17c812424c54413de3333","entities":[],"terms":[]}],
21:28:49 web.1 | path: 'documents' }

案例:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Document = require('./document');

var CaseSchema = ({
documents: [Document]
});

module.exports = mongoose.model('Case', CaseSchema);

文档:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var DocumentSchema = ({
url: String
});

module.exports = mongoose.model('Document', DocumentSchema);

代码:

'use strict';

var mongoose = require('mongoose');
var Case = mongoose.model('Case');
var Document = mongoose.model('Document');

exports.populateCase = function(req, res) {

var caseId = req.params.caseId;

var doc1 = new Document({
url: 'http://www.bbc.co.uk'
});
var doc2 = new Document({
url: 'http://www.google.com'
});

Case.update({_id: caseId}, {$pushAll: { documents: [doc1, doc2] }}, {upsert: true}, function(err) {
if (err) {
console.log(err);
} else {
console.log('YAY!');
}
});
};

最佳答案

您可以尝试以下方法:

案例

var mongoose = require('mongoose'),
Schema = mongoose.Schema
var CaseSchema = ({
documents: [{type: Schema.Types.Object, ref: 'Document' }]
});

var Case = mongoose.model('Case', CaseSchema);
module.exports = Case

文档

var mongoose = require('mongoose'),
Schema = mongoose.Schema

var DocumentSchema = ({
url: String
});

module.exports = mongoose.model('Document', DocumentSchema);

应用程序

var Document = require('./Document')
var Case = require('./Case')

var doc1 = new Document({
url: 'http://www.bbc.co.uk'
});
var doc2 = new Document({
url: 'http://www.google.com'
});


Case.update({_id: caseId}, {$pushAll: { documents: [doc1, doc2] }}, {upsert: true}, function(err) {
if (err) {
console.log(err);
} else {
console.log('YAY!');
}
});

关于node.js - 无法将子文档推送到 Mongoose 中的文档数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31361547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com