Expressでは、私はPOST要求をスキャンし、配列openingTimes
を作成しようとしています。入力に基づいてMongoDBドキュメントを作成します。要求オブジェクト内の文字列を反復処理する方法は?
コードスニペットはうまくいきますが、n日を定義する代わりにループを作るにはどうすればよいですか?
`module.exports.myController = function (request, response) {
MongoDBModel.create({
name: req.body.name,
//I want to make a loop in here, so I dont have to define each day separately
openingTimes: [{
days: req.body.days1
, opening: req.body.opening1
, closing: req.body.closing1
, closed: req.body.closed1
}, {
days: req.body.days2
, opening: req.body.opening2
, closing: req.body.closing2
, closed: req.body.closed2
}]
// catching errors
}, function (err, location) {
if (err) {
sendJsonResponse(res, 400, err);
console.log("error is " + err);
}
else {
sendJsonResponse(res, 201, location);
}
});
};`
'openingTimes'配列には常に正確に2つのオブジェクトがありますか? – Hydrothermal
レスポンスのプロパティは、「opening1、opening2」と正確に名前が付けられていますか? – Saravana
@Hydrothermal配列内には任意の数のオブジェクトを配置できます。実際、柔軟性を保つためにループを作りたいと思います。 – Ali