我刚刚通过 npm 安装了 Sequelize 和 MySQL 模块,但遇到了错误。
我可以成功查询我的模型,但尝试创建新模型实例会导致 query-generator.js 文件中出现错误。
我的代码:
var Sequelize = require('sequelize')
, sequelize = new Sequelize('databasename', 'username', 'password');
var User = sequelize.define('users', {
email: Sequelize.STRING,
fname: Sequelize.STRING,
lname: Sequelize.STRING,
});
// WORKS
User.find(1)
.success(function(user) {
console.log('User found');
}).
error(function(err) {
console.log('Error locating user', err);
});
// FAILS
User.create({
email : 'test@example.com',
fname : 'John',
lname : 'Doe'
})
.success(function(user) {
// Do something
})
.error(function(err) {
// Do something
});
这里是错误
~/Project/node_modules/sequelize/lib/dialects/abstract/query-generator.js:156
return Utils._.template(query)(replacements)
^
TypeError: Object function lodash(value) {
// don't wrap if already wrapped, even if wrapped by a different `lodash` constructor
return (value && typeof value == 'object' && !isArray(value) && hasOwnProperty.call(value, '__wrapped__'))
? value
: new lodashWrapper(value);
} has no method 'template'
at Object.module.exports.QueryGenerator.insertQuery (~/Project/node_modules/sequelize/lib/dialects/abstract/query-generator.js:156:22)
最佳答案
原来我的本地版本的 npm 正在获取 Emmet 特定的 lodash 版本。运行
npm install lodash
获取了这个 repo https://github.com/emmetio/lodash而不是 https://github.com/lodash/lodash
运行
npm install lodash --force
并重新安装 Sequelize 解决了我的问题。
关于mysql - Sequelize 错误 - lodash 没有方法 "template",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23815763/