我使用sequelize有一个全新的Node应用程序,但它不会让我运行单个查询。
我正在尝试find
任何事情而且它不起作用:
var models = require('./models');
models.User.findById(1, { logging: console.log })
每个find
查询最终都会显示完全相同的错误。数据库已迁移并播种。我通过打开它进行了验证,其中有很多用户。
这是我的模特:
// user.js
'use strict';
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
email: DataTypes.STRING,
name: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
User.belongsToMany(models.Group, {through: 'Membership'})
}
}
});
return User;
};
堆栈跟踪:
Executing (default): SELECT `id`, `email`, `name`, `createdAt`, `updatedAt` FROM `Users` AS `User` WHERE `User`.`id` = 1;
/node_modules/sqlite3/lib/trace.js:28
throw err;
^
TypeError: date.indexOf is not a function
at DATE.parse (/node_modules/sequelize/lib/dialects/sqlite/data-types.js:28:14)
at /node_modules/sequelize/lib/dialects/sqlite/query.js:170:36
at /node_modules/lodash/lodash.js:12572:23
at /node_modules/lodash/lodash.js:4361:15
at baseForOwn (/node_modules/lodash/lodash.js:2622:24)
at Function.mapValues (/node_modules/lodash/lodash.js:12571:7)
at /node_modules/sequelize/lib/dialects/sqlite/query.js:134:32
at Array.map (native)
at Statement.afterExecute (/node_modules/sequelize/lib/dialects/sqlite/query.js:133:39)
--> in Database#all('SELECT `id`, `email`, `name`, `createdAt`, `updatedAt` FROM `Users` AS `User` WHERE `User`.`id` = 1;', [], [Function])
at /node_modules/sequelize/lib/dialects/sqlite/query.js:241:36
at Promise._execute (/node_modules/bluebird/js/release/debuggability.js:272:9)
at Promise._resolveFromExecutor (/node_modules/bluebird/js/release/promise.js:473:18)
at new Promise (/node_modules/bluebird/js/release/promise.js:77:14)
at executeSql (/node_modules/sequelize/lib/dialects/sqlite/query.js:103:19)
at tryCatcher (/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/node_modules/bluebird/js/release/promise.js:502:31)
at Promise._settlePromise (/node_modules/bluebird/js/release/promise.js:559:18)
at Promise._settlePromise0 (/node_modules/bluebird/js/release/promise.js:604:10)
at Promise._settlePromises (/node_modules/bluebird/js/release/promise.js:683:18)
at Promise._fulfill (/node_modules/bluebird/js/release/promise.js:628:18)
at MappingPromiseArray.PromiseArray._resolve (/node_modules/bluebird/js/release/promise_array.js:125:19)
at MappingPromiseArray._promiseFulfilled (/node_modules/bluebird/js/release/map.js:97:18)
at Promise._settlePromise (/node_modules/bluebird/js/release/promise.js:564:26)
at Promise._settlePromise0 (/node_modules/bluebird/js/release/promise.js:604:10)
编辑:如果我注释掉堆栈中最顶层的项目(这是一个向后兼容的东西)它工作正常.... ????????????请告诉我,我不是疯了。
答案 0 :(得分:2)
重要的是要注意createdAt和updatedAt默认声明为Sequelize.DATE
。
如果你使用的数据库是mysql / sqlite,那基本上应该是一个日期字符串,正如你在documentation中看到的那样。
我过去遇到了同样的错误,当我使用Date.now()
为我的数据库播种条目时,它为字段createdAt和updatedAt返回了一个数字时间戳:
'use strict';
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.bulkInsert('Pokemons', [
{name: 'bulbasaur', createdAt: Date.now(), updatedAt: Date.now()},/*...*/
]);
},
/*...*/
};
要解决此问题,请将其更改为new Date()
,这将返回日期字符串。
'use strict';
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.bulkInsert('Pokemons', [
{name: 'bulbasaur', createdAt: new Date(), updatedAt: new Date()},/*...*/
]);
},
/*...*/
};
也许这就是你的情况。