[英]Changing a MongoDB query into a Postgres query in Javascript
我在转换此查询时遇到了挑战。 我在其他人身上取得了成功,但这个领域结合了多个领域,我需要一些帮助。 这是为了获取创建预订的日期。
//convert this:
var getByDate = (restId, date) => {
return new Promise((resolve, reject) => {
// create search-between dates for the date sought
var workDate = new Date(date);
var fromDate = new Date(workDate.getFullYear(), workDate.getMonth(), workDate.getDate());
var thruDate = new Date(fromDate);
thruDate.setDate(thruDate.getDate() + 1);
Reservation.find({
restaurant_id: restId
})
.where('reservation_time').gte(fromDate).lt(thruDate)
.exec((err, reservations) => {
resolve(reservations);
});
});
};
//convert into PostgreSql query:
var getByDate = function (restId, date, callback) {
return new Promise((resolve, reject) => {
// create search-between dates for the date sought
var workDate = new Date(date);
var fromDate = new Date(workDate.getFullYear(), workDate.getMonth(), workDate.getDate());
var thruDate = new Date(fromDate);
thruDate.setDate(thruDate.getDate() + 1);
client.query('SELECT * FROM reservationSchema WHERE restaurant_id = restId ;', (error, results) => { //! NEED HELP ON THIS LINE
if (error) {
throw error;
}
callback(results.rows);
})
// .where('reservation_time').gte(fromDate).lt(thruDate) //! old section commented out
// .exec((err, reservations) => {
// resolve(reservations);
// });
});
};
我想出了这个:
var getByDate = function (restId, date, callback) {
return new Promise((resolve, reject) => {
// create search-between dates for the date sought
var workDate = new Date(date);
var fromDate = new Date(workDate.getFullYear(), workDate.getMonth(), workDate.getDate());
var thruDate = new Date(fromDate);
thruDate.setDate(thruDate.getDate() + 1);
client.query('SELECT * FROM reservationSchema WHERE restaurant_id = $1 and reservation_time >= $2, and reservation_time < $3;', [restId, fromDate, thruDate], (error, results) => {
if (error) {
throw error;
}
callback(results.rows);
})
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.