我收到的错误是:
C:\Users\Minh Lu\Desktop\MusicMediaWebApp\database\dbService.js:34
[0] con.query(sql, typeCast: function(field, next) {
[0] ^^^^^^^^
[0]
[0] SyntaxError: missing ) after argument list
由此:
/* Retrieves a User model by ID */
getUserByID: function(ID, callback) {
this.tryConnect().getConnection(function(err, con) {
var sql = queries.getUserByID;
con.query(sql, typeCast: function(field, next) {
// We only want to cast bit fields that have a single-bit in them. If the field
// has more than one bit, then we cannot assume it is supposed to be a Boolean.
if ( ( field.type === "BIT" ) && ( field.length === 1 ) ) {
var bytes = field.buffer();
// A Buffer in Node represents a collection of 8-bit unsigned integers.
// Therefore, our single "bit field" comes back as the bits '0000 0001',
// which is equivalent to the number 1.
return( bytes[ 0 ] === 1 );
}
return next();
}, ID, function (err, result) {
if (err) throw err;
// Call the callback function in the caller of this method so we can do something with this "result"
return callback(result); // [] if not found
});
});
},
我对这是什么语法错误感到困惑?这与文档中的方法相同:https://github.com/mysqljs/mysql#type-casting
谢谢!
答案 0 :(得分:1)
仔细阅读文档。
connection.query({
sql: '...',
typeCast: function (field, next) {
if (field.type == 'TINY' && field.length == 1) {
return (field.string() == '1'); // 1 = true, 0 = false
}
return next();
}
});
connection.query(...)
正在接受一个对象作为参数。