我正在尝试制作类似this的API
我试图向阵列添加控制器,但是执行错误时出现问题。
我一直在寻找来自google和stackoverflow的解决方案,但我对此感到困惑。
我非常感谢您的帮助
这是我来自控制器的代码
getCommunity: async (req, res, next) => {
const { playerId } = req.params;
const newCommunity = new Community(req.body);
//Get Player
const player = Player.findById(playerId);
// Assign player as a part of community's player
newCommunity.communityPlayer = player;
// Save the community
await newCommunity.save();
// Add community to the player's array
player.PlayerCommunities.push(newCommunity);
// Save the playerCommunities
await player.save();
res.status(201).json(newCommunity)
}
这是我的模型播放器
const playerSchema = new Schema({
playerName: String,
playerImage: { type: String, data: Buffer },
playerNumberPhone: {
type: String,
validate: {
validator: function(v) {
return /\d{3}-\d{4}-\d{4}/.test(v);
},
message: props => `${props.value} is not a valid phone number!`
},
required: [true, 'User phone number required']
},
playerAddress: String,
PlayerCommunities: [{type: Schema.Types.ObjectId, ref: 'community'}]
});
这是我的模特社区
const communitySchema = new Schema({
communityName: String,
communityImage: {type: String, data: Buffer},
communityPlayer: [{
type: Schema.Types.ObjectId,
ref: 'player'}]
}, { usePushEach: true });
这实际上是我使用的终点
http://localhost:3000/player/:playerId/community
答案 0 :(得分:0)
将newCommunity播放分配给数组的方式是错误的,这就是为什么它会发出错误。尝试这样
static void commun(String tel1, String tel2) {
for(int i=0;i<tel1.length();i++) {
for(int j=0;j<tel2.length();j++) {
if(tel1.charAt(i)==tel2.charAt(j)) {
System.out.printf(" %c,", tel1.charAt(i));
break;
}
}
}
}
答案 1 :(得分:0)
在社区模型中,communityPlayer属性是一个数组。
但是在控制器中,您正在分配一个非数组值。
更改控制器的下一行
newCommunity.communityPlayer = player;
收件人
newCommunity.communityPlayer.push(player);