gpt4 book ai didi

javascript - 风 sails js : can't create dummy object on bootstrap with Date attribute

转载 作者:行者123 更新时间:2023-11-27 23:55:17 25 4
gpt4 key购买 nike

在添加“日期”属性之前,我可以在 Bootstrap 中创建一个虚拟用户对象。在 bootstrap.js 中我有:

module.exports.bootstrap = function(cb) {
var dummyData = [
{
"firstName":"Jane",
"lastName":"Doe",
"dateofbirth": 1279703658 //timestamp
}
]

User.count().exec(function(err, count){
if(err){
return cb(err);
}

if(count == 0){
User.create(dummyData).exec(function(){
cb();
});
}
});
};

User.js 很简单,如下所示:

module.exports = {

attributes: {
firstName : {
type : 'string',
required : true
},
lastName : {
type : 'string',
required : true
},

dateofbirth : {
type : 'date'
}
}
};

当我尝试在浏览器中创建相同的对象(具有良好的 CRUD 功能)时,我收到有关日期的错误:

{
"error": "E_VALIDATION",
"status": 400,
"summary": "1 attribute is invalid",
"model": "User",
"invalidAttributes": {
"dateofbirth": [
{
"rule": "date",
"message": "`undefined` should be a date (instead of \"123454345\", which is a string)"
}
]
}
}

所以问题是如何创建具有日期属性的此类对象?

最佳答案

date(或等效的dateTime)接受例如ISO 日期字符串。因此,您的示例可能如下所示:

var dummyData = [
{
"firstName":"Jane",
"lastName":"Doe",
// "1970-01-15T19:28:23.658Z"
"dateofbirth": new Date(1279703658).toISOString()
}
]

关于javascript - 风 sails js : can't create dummy object on bootstrap with Date attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33221918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com