datePosted 始终为 ‘0000-00-00 00:00:00’

huangapple go评论88阅读模式
英文:

datePosted always '0000-00-00 00:00:00'

问题

这是我用于编写评论的JavaScript代码。以前它可以工作,但现在'日期发布'列总是给我默认的'0000-00-00 00:00:00'输出。

writeReview(request, respond) {
    var now = new Date();
    var reviewObject = new Review(null, request.body.reviewUserId, request.body.reviewResId, request.body.review,
        request.body.rating, request.body.numberOfLikes, now.toString());
    var sql = "INSERT INTO resreview.reviews (reviewUserId, reviewResId, review, rating, numberOfLikes, datePosted) VALUES(?,?,?,?,?,?)";

    var values = [reviewObject.getReviewUserId(), reviewObject.getReviewResId(), reviewObject.getReview(),
    reviewObject.getRating(), reviewObject.getNumberOfLikes(), reviewObject.getDatePosted()];
    db.query(sql, values, function (error, result) {
        if (error) {
            throw error;
        }
        else {
            respond.json(result);
        }
    });
}

这是我的'Review.js'文件的内部:

"use strict"

class Review {
    constructor(reviewId, reviewUserId, reviewResId, review, rating, numberOfLikes, datePosted) {
        this.reviewId = reviewId;
        this.reviewUserId = reviewUserId;
        this.reviewResId = reviewResId;
        this.review = review;
        this.rating = rating;
        this.numberOfLikes = numberOfLikes;
        this.datePosted = datePosted;
    }
    // 添加获取方法
    getReviewId() {
        return this.reviewId;
    }
    getReviewUserId() {
        return this.reviewUserId;
    }
    getReviewResId() {
        return this.reviewResId;
    }
    getReview() {
        return this.review;
    }
    getRating() {
        return this.rating;
    }
    getNumberOfLikes() {
        return this.numberOfLikes;
    }
    getDatePosted() {
        return this.datePosted;
    }
    // id会自动递增
    setReviewId(reviewId) {
        this.reviewId = reviewId;
    }
    setReviewUserId(reviewUserId) {
        this.reviewUserId = reviewUserId;
    }
    setReviewResId(reviewResId) {
        this.reviewResId = reviewResId;
    }
    setReview(review) {
        this.review = review;
    }
    setRating(rating) {
        this.rating = rating;
    }
    setNumberOfLikes(numberOfLikes) {
        this.numberOfLikes = numberOfLikes;
    }
    setDatePosted(datePosted) {
        this.datePosted = datePosted;
    }
}
module.exports = Review;

这是我在MySQL数据库中显示的数据。

我已经检查过我的以前工作的文件,代码似乎没有问题。我不是使用PHP,我在这里使用DATETIME作为我的数据类型,不像我的以前的文件,datePosted列使用VARCHAR,这可能是问题吗?谢谢你的帮助!

英文:

This is my javascript code for writing reviews. It used to work, but now the 'datePosted' column always gives me the default '0000-00-00 00:00:00' output.

writeReview(request, respond) {
var now = new Date();
var reviewObject = new Review(null, request.body.reviewUserId, request.body.reviewResId, request.body.review,
request.body.rating, request.body.numberOfLikes, now.toString());
var sql = "INSERT INTO resreview.reviews (reviewUserId, reviewResId, review, rating, numberOfLikes, datePosted) VALUES(?,?,?,?,?,?)";
var values = [reviewObject.getReviewUserId(), reviewObject.getReviewResId(), reviewObject.getReview(),
reviewObject.getRating(), reviewObject.getNumberOfLikes(), reviewObject.getDatePosted()];
db.query(sql, values, function (error, result) {
if (error) {
throw error;
}
else {
respond.json(result);
}
});
}

This is the internals for my 'Review.js' file:

"use strict"
class Review {
constructor(reviewId, reviewUserId, reviewResId, review, rating, numberOfLikes, datePosted) {
this.reviewId = reviewId;
this.reviewUserId = reviewUserId;
this.reviewResId = reviewResId;
this.review = review;
this.rating = rating;
this.numberOfLikes = numberOfLikes;
this.datePosted = datePosted;
}
//add the get methods here
getReviewId() {
return this.reviewId;
}
getReviewUserId() {
return this.reviewUserId;
}
getReviewResId() {
return this.reviewResId;
}
getReview() {
return this.review;
}
getRating() {
return this.rating;
}
getNumberOfLikes() {
return this.numberOfLikes;
}
getDatePosted() {
return this.datePosted;
}
//id is automatically-incremented
setReviewId(reviewId) {
this.reviewId = reviewId;
}
setReviewUserId(reviewUserId) {
this.reviewUserId = reviewUserId;
}
setReviewResId(reviewResId) {
this.reviewResId = reviewResId;
}
setReview(review) {
this.review = review;
}
setRating(rating) {
this.rating = rating;
}
setNumberOfLikes(numberOfLikes) {
this.numberOfLikes = numberOfLikes;
}
setDatePosted(datePosted) {
this.datePosted = datePosted;
}
}
module.exports = Review;

This is my data shown in MySQL database.
datePosted 始终为 ‘0000-00-00 00:00:00’

I have checked with my previously working files and there seems to be no issue with the codes. And I am not using PHP for this. Here I am using DATETIME as my datatype unlike my previous files which is VARCHAR for the datePosted column, could that be the issue? Thanks for your help!

答案1

得分: 2

将其转换为MySQL datetime,如评论部分所述。或者,由于您使用当前日期,只需在SQL语句本身中使用"Now()"(不带引号)作为值。

英文:

You should convert it to mysql datetime as mentioned in the comment section. Or, since you use current date, just use "Now()" (without quotes) as value in the sql statement itself.

huangapple
  • 本文由 发表于 2020年1月3日 17:40:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定