Это мой запрос на размещение в Unity:
UnityWebRequest request = UnityWebRequest.Put(baseUrl + "miniGame1s/", JsonUtility.ToJson(cardsToSend));
А "cardsToSend" - это класс формы объекта ниже:
[Serializable]
class Cards
{
public string uid;
public List<Card> cards = new List<Card>();
}
[Serializable]
class Card
{
public int index;
public int theCard;
public bool theAnswer;
public int theState;
public bool playerAnswer;
public float answerMoment;
public float timeAfterAnswer;
public float scoreAfterAnswer;
public int validOrNot;
}
А это код на сервере, которыйнаписано в Node js и Express:
app.put('/api/miniGame1s',(req,res)=>{
console.log(req.body);
const objectSchema = Joi.object().keys({
index: Joi.number().required(),
theCard: Joi.number().required(),
theAnswer: Joi.boolean().required(),
theState: Joi.number().required(),
playerAnswer: Joi.boolean().required(),
answerMoment: Joi.number().required(),
timeAfterAnswer: Joi.number().required(),
scoreAfterAnswer: Joi.number().required(),
validOrNot: Joi.number().integer().required()
});
const arraySchema = {
uid: Joi.string().required(),
cards: Joi.array().items(objectSchema)
};
const result = Joi.validate(req.body, arraySchema);
if(result.error) return res.status(400).send(result.error.details[0].message);
else{
thatGame1InProgressIndex = game1sInProgress.findIndex(g => g.uid == req.body.uid);
console.log('Index: ' + thatGame1InProgressIndex);
for(let i = 0; i < req.body.cards.length;i++)
{
game1sInProgress[thatGame1InProgressIndex].cards[req.body.cards[i].index] = req.body.cards[i];
}
// TODO: validating incomming info and set the validation parameter
// TODO: generate new cards and send them to client.
let newCards = {
uid: req.body.uid,
cards: []
};
for(let i = 0; i < 10 ;i++)
{
let card = CreateACard(game1sInProgress[thatGame1InProgressIndex].cards[game1sInProgress[thatGame1InProgressIndex].cards.length-1].theCard, game1sInProgress[thatGame1InProgressIndex].cards.length);
game1sInProgress[thatGame1InProgressIndex].cards.push(card);
newCards.cards.push(card);
}
res.send(newCards);
}
});
Теперь код сервера еще не завершен.(это не проблема) проблема в том, что я не могу получить объект JSON на сервере.
Я пробовал разные вещи, такие как использование ToString () в единице вместо JSON.ToJson (), или синтаксический анализ "req.body" на сервере с помощью JSON.parse (), или подобные вещи.
В лучшем случае я дам ошибку: требуется «uid», то есть ошибка проверки Joi.это означает, что он не может раскрыть параметры объекта.
Я пытался отправить тот же запрос с теми же данными на сервер с приложением POSTMAN.и это работает.
Я думаю, что проблема в том, что структура JSON, созданного JSONUtility, является проблемой.
Любая помощь будет оценена.