我正在为我的项目使用NodeJS,Express和MySQL,并希望使用Bookshelf ORM.
Bookshelf使用Knex进行查询,建模,并建议通过Knex(http://bookshelfjs.org/#installation)设置数据库连接.
我在与Knex建立成功的数据库连接时遇到了麻烦.我想只在数据库连接成功时启动服务器,但似乎在建立连接之后它没有提供任何东西(没有承诺或属性).
这是我一直在使用的代码.
import _knex from "knex"; // npm install knex --save
import _bookshelf from "bookshelf"; // npm install bookshelf --save
let knex = _knex({
client: "mysql",
connection: {
host: "127.0.0.1",
database: process.env.DB,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD
},
debug: true
});
let bookshelf = _bookshelf(knex);
module.exports.knex = knex;
module.exports.bookshelf = bookshelf;
Run Code Online (Sandbox Code Playgroud)
更多参考:还有另一个名为Sequelize的ORM,它提供sequelize.authenticate()哪些返回Promise并可以用作(http://docs.sequelizejs.com/en/latest/api/sequelize/#authenticate-promise)
sequelize.authenticate()
.then( () => {
console.log("Db successfully connected");
app.listen(port, () => console.log(`App started at: ${port}`) );
})
.catch( err …Run Code Online (Sandbox Code Playgroud) 我希望这个切换能够工作,但不知何故,组件的构造函数<A/>仅被调用一次。https://codesandbox.io/s/jvr720mz75
import React, { Component } from "react";
import ReactDOM from "react-dom";
class App extends Component {
state = { toggle: false };
render() {
const { toggle } = this.state;
return (
<div>
{toggle ? <A prop={"A"} /> : <A prop={"B"} />}
<button onClick={() => this.setState({ toggle: !toggle })}>
toggle
</button>
</div>
);
}
}
class A extends Component {
constructor(props) {
super(props);
console.log("INIT");
this.state = { content: props.prop };
}
render() {
const { content …Run Code Online (Sandbox Code Playgroud) 我有一个角度范围的材料日期选择器(开始日期和结束日期)。
目前,它是自由选择的。意思是,我可以选择任何开始日期和任何结束日期。我想稍微改变一下。我希望它限制最多 7 天的差异。我不想让用户选择天数差超过 7 的 2 个日期。
所以在日历里面:它看起来像这样
如您所见,10 月 5 日是开始日期,它允许我们选择 10 月 17 日作为结束日期。但我希望用户只能在 10 月 5 日至 10 月 12 日(最多 7 天差异)范围内选择结束日期。
有办法吗?
这是我的 HTML:
<mat-form-field class="datepicker" appearance="fill">
<mat-label>Enter a date range</mat-label>
<mat-date-range-input [formGroup]="rangeForm" [rangePicker]="picker" [max]="maxDate">
<input matStartDate formControlName="start" placeholder="Start date" readonly>
<input matEndDate formControlName="end" placeholder="End date" readonly>
</mat-date-range-input>
Run Code Online (Sandbox Code Playgroud)
打字稿:
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import * as moment from 'moment';
@Component({
selector: 'app-chart',
templateUrl: …Run Code Online (Sandbox Code Playgroud) 所以我使用react-table库来显示一个带有模拟数据的树形网格表,但它看起来并不像它应该的那样,它表明表上有一个项目。
import React, { Component } from 'react';
import ReactTable from "react-table";
import 'react-table/react-table.css'
export default class TestTable extends Component {
state = {
data: [{
actionNo: "1",
action: "--",
productService: "Mobile Contract",
qty: 1,
startDate: Date.now(),
endDate: Date.now(),
account: 11111111,
mobileNo: 9111111,
amount: "--",
status: "Error"
}]
}
render() {
const { data } = this.state;
console.log(data);
const columns = [{
Header: 'Action No.',
accessor: 'actionNo'
}, {
Header: 'Action',
accessor: 'action',
}, {
acessor: 'productService',
Header: …Run Code Online (Sandbox Code Playgroud) 我正在下载一个网页,然后thisArticle.html使用以下代码写入名为 的文件。
var file = fs.createWriteStream("thisArticle.html");
var request = http.get(req.body.url, response => response.pipe(file) );
Run Code Online (Sandbox Code Playgroud)
之后,我尝试读取文件并上传到 S3,这是我编写的代码:
fs.readFile('thisArticle.html', 'utf8', function(err, html){
if (err) {
console.log(err + "");
throw err;
}
var pathToSave = 'articles/ ' + req.body.title +'.html';
var s3bucket = new AWS.S3({ params: { Bucket: 'all-articles' } });
s3bucket.createBucket(function () {
var params = {
Key: pathToSave,
Body: html,
ACL: 'public-read'
};
s3bucket.upload(params, function (err, data) {
fs.unlink("thisArticle.html", function (err) {
console.error(err);
});
if (err) {
console.log('ERROR MSG: …Run Code Online (Sandbox Code Playgroud) 我有一个在端口 3000 和 4000 上运行的 Express js 服务器\n并且想要从服务器 3000 向服务器 4000 发送 post 请求
\n\n我试过这个:
\n\nvar post_options = {\n url: "http://172.28.49.9:4000/quizResponse",\n timeout: 20000,\n method:"POST",\n encoding: "application/json; charset=utf-8",\n body : {data: formdata}\n};\nrequest(post_options,\nfunction optionalCallback(err, httpResponse, body) {\n if (err) {\n console.log(err);\n }else\n console.log(body);\n});\nRun Code Online (Sandbox Code Playgroud)\n\n但出现此错误:
\n\n\n\n\n类型错误:第一个参数必须是 Request.write 处 ClientRequest.OutgoingMessage.write (_http_outgoing.js:456:11) 处的字符串或缓冲区 (D:\\restfullApi\\examineerapi\\node_modules\\request\\request.js\ xe2\x80\x8c\xe2\x80\x8b:1514:27) 在末尾 (D:\\restfullApi\\examineerapi\\node_modules\\request\\request.js\xe2\x80\x8c\xe2\x80\x8b :552:18) 立即。(D:\\restfullApi\\examineerapi\\node_modules\\request\\request.js\xe2\x80\x8c\xe2\x80\x8b:581:7) 在 runCallback (timers.js:637:20) 在 tryOnImmediate (timers.js:610:5) 在 processImmediate [as _immediateCallback] (timers.js:582:5)
\n
这并不像我想象的那样工作。请帮我解决这个问题。
\nUnhandledPromiseRejectionWarning 在异步等待中
我有以下代码:
function foo() {
return new Promise((resolve, reject) => {
db.foo.findOne({}, (err, docs) => {
if (err || !docs) return reject();
return resolve();
});
});
}
async function foobar() {
await foo() ? console.log("Have foo") : console.log("Not have foo");
}
foobar();
Run Code Online (Sandbox Code Playgroud)
结果如下:
(节点:14843)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):false
(节点:14843)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零的退出代码终止Node.js进程。
注意:我知道我可以解决以下问题:
foo().then(() => {}).catch(() => {});
Run Code Online (Sandbox Code Playgroud)
但是,然后我们“回到”回调异步样式。
我们如何解决这个问题?
require()我知道当在浏览器中调用该函数而不是在节点内调用该函数时会发生此错误。但是,我似乎不明白我到底需要做什么来解决这个问题。任何帮助将不胜感激。您可以访问以下存储库以获取整个代码库https://github.com/thegreekjester/React_SSR。
运行并重现问题的步骤:
localhost:3000在浏览器中打开Webpack.client.js
const path = require('path');
const webpackNodeExternals = require('webpack-node-externals');
module.exports = {
// production || development
mode: 'development',
// Inform webpack that we're building a bundle
// for nodeJS, rather then for the browser
target: 'node',
// Tell webpack the root file of our
// server application
entry: './src/client.js',
// Tell webpack where to put the output file
// that is generated
output: {
filename: 'client_bundle.js',
path: path.resolve(__dirname, …Run Code Online (Sandbox Code Playgroud) javascript ×5
node.js ×3
reactjs ×3
amazon-s3 ×1
angular ×1
bookshelf.js ×1
datepicker ×1
express ×1
knex.js ×1
react-redux ×1
react-table ×1
redux ×1
server ×1
webpack ×1