const { mysql: config } = require(’…/config’);
var knex = require(‘knex’)({
client: ‘mysql’,
connection: {
host: config.host,
port: config.port,
user: config.user,
password: config.pass,
database: config.db,
charset: config.char,
multipleStatements: true
}
});
knex(‘test’).insert({ id: ‘1’, name: ‘Tom’, sex: ‘male’, age: ‘18’ }).returning(’*’);
@Jason 也是数据库这里弄了很久还是搞不定,前端总是没数据或者出错,帮忙看下。
另外,后端的代码上传到了腾讯云,怎么查看到node打印出来的东西啊?
module.exports = async (ctx, next) => { if (ctx.state.$wxInfo.loginState === 1) { /*const { mysql: config } = require('../config'); var knex = require('knex')({ client: 'mysql', connection: { host: config.host, port: config.port, user: config.user, password: config.pass, database: config.db, charset: config.char, multipleStatements: true } }); knex('question_sort').select('id').then(res => { ctx.state.data = res; })*/ const { mysql } = require( '../qcloud' ) mysql( 'question_sort' ).select( 'id' ).then(res => { console.log(res) ctx.state.data = res; }) } else { ctx.state.code = -1 } } |
楼主,求教,这部分操作数据库的源码,写在哪?官方下载下来的源码的:
\wafer2-startup-2.0.1\server\tools\initdb.js里面?
在哪里操作?
比如我在页面上的按钮上点击,要怎么引入这个initdb.js里面的操作数据库这段?
谢谢回复了
knex返回的是一个promise,所以应该这么使用:
knex(‘test’).insert({ id: ‘1’, name: ‘Tom’, sex: ‘male’, age: ‘18’ }).returning(’*’).then(res => { console.log(res) })
另外建议支持使用 SDK 导出的 MySQL 实例:
const { mysql } = require(’…/qcloud’)
mysql(‘test’).insert({ id: ‘1’, name: ‘Tom’, sex: ‘male’, age: ‘18’ }).returning(’*’).then(res => { console.log(res) })