将插入值快速表达到MySQL数据库中 [英] Express insert values into MySQL database

查看:91
本文介绍了将插入值快速表达到MySQL数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题已解决.请看下面.

PROBLEM SOLVED. Please look below.

我是Express和NodeJS的新手,抛弃了Laravel和PHP.

I'm new into Express and NodeJS, ditched Laravel and PHP.

我想要做的是能够将记录添加到MySQL数据库中,但是我无法将点连接起来.我正在关注本教程系列:

What I want to do is to be able to add a record into MySQL database, but I am not able to connect the dots. I'm following this tutorial series :

http://eddyjs.com/bookshelf-js/

http://eddyjs.com /using-mysql-with-bookshelf-js-part-2-using-the-database/

有两个db变量,我不明白如何使用它们.

There are two db variables, I couldn't understand how to use them.

这是错误.

无法读取未定义的属性扩展"

Cannot read property 'extend' of undefined

TypeError:无法读取未定义的属性"extend" 在对象. (/Applications/MAMP/htdocs/myapp/myapp/models/User.js:5:20) 在Module._compile(module.js:460:26) 在Object.Module._extensions..js(module.js:478:10) 在Module.load(module.js:355:32) 在Function.Module._load(module.js:310:12) 在Module.require(module.js:365:17) 在要求时(module.js:384:17)

TypeError: Cannot read property 'extend' of undefined at Object. (/Applications/MAMP/htdocs/myapp/myapp/models/User.js:5:20) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) at require (module.js:384:17)

我通过npm安装了所有依赖项,一切正常.

I installed all dependencies via npm, everything is ok.

  "dependencies": {
    "body-parser": "~1.13.2",
    "bookshelf": "^0.8.2",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "express": "~4.13.1",
    "jade": "~1.11.0",
    "jquery": "^2.1.4",
    "knex": "^0.8.6",
    "morgan": "~1.6.1",
    "mysql": "^2.9.0",
    "serve-favicon": "~2.3.0"
  }

我将模型保存在模型文件夹中.

I hold my models in models folder.

User.js

var db = require('./db');

var User = db.Model.extend({
    tableName: 'users'
});

module.exports = User;

routes/index.js

routes/index.js

router.get('/add', function(req,res,next) {
  var User = require('../models/User');
  new User({
    'name': 'Edwin',
    'pet': 'dog'
  })
      .save()
      .then(function (newUser) {
        console.log('user created!', newUser);
      });
});

db.js(数据库连接应该打开一次,对吧?因此它必须存在于此处)

db.js (database connection should be opened once, right? So it has to live here)

   var knex = require('knex')({
    client: 'mysql',
    connection: {
        host     : 'localhost',
        user     : 'root',
        password : 'root',
        port    : 8889,
        database : 'databasename',
        charset  : 'utf8'
    }
});

var bookshelf = require('bookshelf')(knex);

var User = bookshelf.Model.extend({
  tableName: 'users'
});

推荐答案

更改db.js时已解决问题.关键是module.exports = db;我猜是一部分.

Problem solved when I change db.js. The key is the module.exports = db; part, I guess.

var knex = require('knex')({
    client: 'mysql',
    connection: {
        host     : 'localhost',
        user     : 'root',
        password : 'root',
        port    : 8889,
        database : 'bosluk',
        charset  : 'utf8'
    }
});

var db = require('bookshelf')(knex);

module.exports = db;

这篇关于将插入值快速表达到MySQL数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆