在学习廖老师的node-mysql-使用Sequelize时,按照老师的代码敲出来,运行的时候一直报错
sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators node_modules\sequelize\lib\sequelize.js:242:13
F:\杂\廖雪峰\node\mysql\hello-sequelize\app.js:64
(async () =>{
^
TypeError: (intermediate value)(...) is not a function
at Object.<anonymous> (F:\杂\廖雪峰\node\mysql\hello-sequelize\app.js:64:1)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
百度了很长时间,都没有解决,于是在廖老师的github上直接复制了他的代码,发现竟然可以运行。在此之前,已经比较过很多遍。在自己用肉眼无法比较出差异之后,用beyond compare比较了一下,发现只是存在是否加;的差异。
const Sequelize = require('sequelize');
const config = require('./config');
// 创建一个sequelize对象实例
var sequelize = new Sequelize(config.database, config.username, config.password,{
host: config.host,
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 30000
}
});
// 定义模型Pet,告诉sequelize如何映射数据库表
// sequelize返回的对象是promise
var Pet = sequelize.define('pet', {
// 传入名称写单数,对应默认的表名为复数
id: {
type: Sequelize.STRING(50),
primaryKey: true
},
name: Sequelize.STRING(100),
gender: Sequelize.BOOLEAN,
birth: Sequelize.STRING(10),
createdAt: Sequelize.BIGINT,
updatedAt: Sequelize.BIGINT,
version: Sequelize.BIGINT
}, {
// 关闭sequelize自动添加timestamps的功能
timestamps: false
});
console.log(Date.now());
var now =Date.now();
Pet.create({
id: 'g-' + now,
name: 'Gaffey',
gender: false,
birth: '2007-07-07',
createdAt: now,
updatedAt: now,
version: 0
}).then(function (p) {
console.log('created.' + JSON.stringify(p));
}).catch(function (err) {
console.log('failed: ' + err);
});
(async ()=>{
var dog = await Pet.create({
id: 'd-' + now,
name:'333',
gender: false,
birth: '2008-08-09',
createdAt: now,
updatedAt: now,
version:0
});
console.log('created' + JSON.stringify(dog))
})();
(async () =>{
var pets = await Pet.findAll({
where: {
name: 'Gaffey'
}
})
console.log(`find ${pets.length} pets`)
for (let p of pets) {
console.log(JSON.stringify(p))
}
})()
我之前在第二个立即执行函数后面没有加;导致了报错。具体原因经百度是这样的:
真正会导致上下行解析出问题的 token 有 5 个:括号,方括号,正则开头的斜杠,加号,减号。我还从没见过实际代码中用正则、加号、减号作为行首的情况,所以总结下来就是一句话:一行开头是括号或者方括号的时候加上分号就可以了,其他时候全部不需要
意思是括号开头会导致上下文解析错误,所以必须在之前加;
具体内容可以参考: