gpt4 book ai didi

node.js - Actor 错误: Cast to Array failed for value

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

当我收到来自帖子的数据并尝试解析它时,出现错误。

这是有关问题的详细信息:

我有两个模型:

var tags = require('../tag/tag.model'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;

var OpportunitiesSchema = new Schema({
employeeTitle: String,
tagline: Boolean,
description:String,
location:String,
isPreferred:Boolean,
tags: [
{
type:Schema.Types.ObjectId,
ref:'Tag'
}
]
});

module.exports.opportunities = mongoose.model('Opportunities', OpportunitiesSchema);

我还有另一个模型:

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

var TagSchema = new Schema({
id: Number,
name: String
});

module.exports = mongoose.model('Tag', TagSchema);

我的功能是这样的:

var Opportunities = require('./opportunities.model').opportunities;
exports.create = function(req, res) {
//addOpportunitites.save(req.body, function(err, tag) {
// if(err) { return handleError(res, err); }
// return res.json(201, tag);
//});

var newOpportunity = new Opportunities(req.body);

newOpportunity.save(function(err) {
if(err) {
console.log(err);
} else {
return res.json(201);
}

})
};

当我发送这样的数据时:

{
"employeeTitle": "Digital Agency Employee, downtown Chicago",
"tagline": "My company is offering $1000 referral bonus and I'll give you half!",
"description": "<p>Looking for someone who has the following abilities: <ul><li></li></ul></p>",
"location": "",
"tags": [
545454,45467
]
}

有一个错误:

{
[ValidationError: Opportunities validation failed]
message: 'Opportunities validation failed',
name: 'ValidationError',
errors: {
tags: {
[CastError: Cast to Array failed for value "545454,45467" at path "tags"]
message: 'Cast to Array failed for value "545454,45467" at path "tags"',
name: 'CastError',
kind: 'Array',
value: [Object],
path: 'tags',
reason: undefined
}
}
}

最佳答案

tags中,您发送Numbers,大概是标签模型中的id,但在Opportunities 模型,您存储的是 ObjectId,它是 mongo 生成的 _id

关于node.js - Actor 错误: Cast to Array failed for value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36451887/

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