gpt4 book ai didi

node.js - Mongoose CastError : Cast to undefined failed for value "[object Object]" at path "apps"

转载 作者:可可西里 更新时间:2023-11-01 09:26:24 29 4
gpt4 key购买 nike

我有一个用 mongoose 定义的嵌套模式:

//application.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Category = require('./category.js');

var Application = new Schema({
title : String,
cats : [Category]
});

Application.virtual('app_id').get(function() {
return this._id;
});

module.exports = mongoose.model('Application', Application);

//account.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');
var Application = require('./application.js');

var Account = new Schema({
username: String,
password: String,
apps: [Application]
});

Account.plugin(passportLocalMongoose);

module.exports = mongoose.model('Account', Account);

现在,如果我尝试推送到 apps,它是 account 中的一个数组,如下所示:

app.post('/application', function(req,res){
var name = req.user.username;
var newApp = new Application();
newApp.title = req.body.title;
console.log(newApp);

Account.findOneAndUpdate({username : name},
{$push: {apps: newApp}},
{safe: true, upsert: true},
function(err, model){
if (err){
console.log(model);
console.error("ERROR: ", err);
res.status(500).send(err);
}else{
res.status(200).send({"status":"ok"});
}
}
);
});

我得到错误:

{ title: 'dogs', _id: 564f1d1444f30e0d13e84e7b, cats: [] }
undefined
ERROR: { [CastError: Cast to undefined failed for value "[object Object]" at path "apps"]
message: 'Cast to undefined failed for value "[object Object]" at path "apps"',
name: 'CastError',
type: undefined,
value: [{"title":"dogs","_id":"564f1d1444f30e0d13e84e7b","cats":[]}],
path: 'apps' }

我做错了什么?

编辑:

在这个 question 中找到了答案实际上我需要导入模式而不是对象

//account.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');
var ApplicationSchema = require('./application.js').schema; //<-- .schema was added

var Account = new Schema({
username: String,
password: String,
apps: [ApplicationSchema]
});

Account.plugin(passportLocalMongoose);

module.exports = mongoose.model('Account', Account);

最佳答案

要在 Account 模型的应用嵌入文档字段中保存 Application 引用,将 _id 值推送到 Application 模型保存时的回调:

account.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');
var Application = require('./application.js');

var Account = new Schema({
username: String,
password: String,
apps: [{type: Schema.Types.ObjectId, ref: 'Application'}]
});

Account.plugin(passportLocalMongoose);
module.exports = mongoose.model('Account', Account);

app.js

app.post('/application', function(req, res){
var name = req.user.username;
var newApp = new Application({
title: req.body.title
});
console.log(newApp);

newApp.save(function (err){
if (err) return handleError(err);

Account.findOne({ username: name }, function (err, doc){
if (err) return handleError(err);
doc.apps.push(newApp._id);
doc.save();
res.status(200).send({"status":"ok"});
});
});
});

或者使用 promises 作为:

const handleError = err => console.error(err);

app.post('/application', (req, res) => {
const name = req.user.username;
const newApp = new Application({
title: req.body.title
});

newApp.save().then(result => (
Account.findOneAndUpdate(
{ "username": name },
{ "$push": { "apps": result._id } }
)
)
.then(result => res.status(200).send({"status":"ok"}))
.catch(handleError);
});

关于node.js - Mongoose CastError : Cast to undefined failed for value "[object Object]" at path "apps",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33828182/

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