当我尝试发送包含多个同名字段的表单数据时,得到的结果是"TypeError:期望的字符串,但收到的是数组“。
我认为问题出在邮递员身上,我希望有多个参与者字段,这些字段将被添加到应该添加到数组中。
// this is from models/Battle
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema
const BattleSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
date: {
type: Date,
default: Date.now
},
category: {
type: Number,
required: true // this will come from the selected category
},
winner: {
type: Number,
default: 0
},
status: {
type: Number,
default: 0 // 0 means the battle is closed, 1 means the battle is open for votes, the status will stay 0 until all participants dropped
},
participants: [
{
participant: {
type: Schema.Types.ObjectId,
required: true
}
}
]
});
module.exports = Battle = mongoose.model('battles', BattleSchema);
//this is from routes/api/battles
// @route POST api/battles
// @desc Create battle
// @access Private
router.post(
'/create-battle',
passport.authenticate('jwt', { session: false }),
(req, res) => {
const { errors, isValid } = validateBattleInput(req.body);
// Check Validation
if (!isValid) {
// If any errors, send 400 with errors object
return res.status(400).json(errors);
console.log(errors);
}
const newBattle = new Battle({
user: req.user.id,
category: req.body.category,
participant: req.body.participant
});
//save
newBattle.save().then(battle => {
// const participant = req.body.participant;
const participant = req.body.participant;
// add participants to array
battle.participants.push( participant );
console.log(typeof req.body.participant);
// get the inserted id
const battleId = battle._id;
res.json(battle);
});
}
);
// this is battle validation
const Validator = require('validator');
const isEmpty = require('./is-empty');
var bodyParser = require('body-parser');
module.exports = function validateBattleInput(data) {
let errors = {};
data.category = !isEmpty(data.category) ? data.category : '';
data.participant = !isEmpty(data.participant) ? data.participant : '';
if (Validator.isEmpty(data.category)) {
errors.category = 'Category field is required';
}
// if (Validator.isEmpty(data.challenger)) {
// errors.challenger = 'Challenger field is required';
// }
if (Validator.isEmpty(data.participant)) {
errors.participant = 'Participant field is required';
}
return {
errors,
isValid: isEmpty(errors)
};
};
发布于 2019-04-20 06:00:35
TypeError:应为字符串,但收到了数组。-在postman和终端窗口中抛出错误。我怀疑可能是用户模式定义不匹配
请检查您的用户模型用户架构,例如名称:{ type: String,required: true }它接收到的内容比预期的要多。
发布于 2019-02-06 08:57:48
在"body“选项卡中,选择"raw",然后在右侧,选择"JSON (application/json)”而不是"text“。
我假设您的API端点使用JSON而不是url编码的表单数据,因为您正在使用express和mongoose运行API。但如果不是这样,你应该在这个问题上澄清这一点。
编写一个合适的JSON主体,我的意思是,对键使用双引号,如下所示:
{"model": { "property": "value", "property2": 1}}
并尝试使用包装对象{"model": <YOUR BODY HERE>}
或不使用包装对象,看看哪种包装适合您,因为包装对象是典型的做法,但有时人们并不使用它们。(在您的代码中看到这一点:req.body.participant
使我认为您可能没有)。
(PS:与问题无关,但我个人更喜欢使用ARC或Insomnia作为rest客户端,因为它们的界面更整洁)
发布于 2019-02-06 12:32:00
如果要在参与者数组中发送数据,则所有字段都应为参与者,而不是参与者
尝试通过原始数据发送数据,然后选择应用程序/数据以获得更好的格式
https://stackoverflow.com/questions/54545068
复制相似问题