Tag: javascript

Node.js中的模型文件不会考虑configuration

我有这样的模型文件,如下所示: 'use strict' module.exports = function (sequelize, DataTypes) { let User = sequelize.define('user', { id: { type: DataTypes.INTEGER(11), allowNull: false, primaryKey: true, autoIncrement: true } }, { classMethods: { associate: function(models) { this.belongsToMany(models.role, { through: { model: 'user_role' }, foreignKey: 'user_id' }) } } }, { tableName: 'user', timestamps: false, underscored: true } ) return […]

React defaultValue不工作axios传递dynamic数据

你好即时新在React和即时通讯尝试玩一点与反应,但inheritance人一点我不明白。 首先,获取axios数据谁返回我的数据,下面,然后我尝试把它们放入input字段,值(和只读),defaultValue更好,现在我有问题,我什么都看不到,值存在当我用萤火虫查看时,奇怪的是,当我添加一个不需要的字符input得到我想要的填充,但不是默认情况下。 非常奇怪的是,当我把所有东西放在一个数组中,并且在它上面执行一个映射函数时,我有这个值 json代码 {"firma":"hallo","strasse":"musterweg 7","plz":"01662"} js代码 import React from 'react'; import ReactDOM from 'react-dom'; import axios from 'axios'; class Testx extends React.Component { constructor(props) { super(props); this.state = { data:[] }; } componentDidMount(){ var self = this; axios.get('http://localhost/index.php') .then(function (response) { self.setState({ data: response.data}); }) .catch(function (error) { console.log(error); }); } render() { return […]

在文件资源pipe理器中无法打开子目录? – Node.js

我不明白为什么我不能在Node.js中打开“我的文档”的子目录。 我目前的代码: function fileExplorer() { var spawn = require('child_process').spawn, child = spawn('C:\\Windows\\explorer.exe', ["%userprofile%\\documents"]); } 完全正常工作来启动用户的“我的文档”文件夹。 但是,如果我想在“我的文档”里面打开一个目录,比如说“我的文档/附加资料”,以下是不起作用的: function fileExplorer() { var spawn = require('child_process').spawn, child = spawn('C:\\Windows\\explorer.exe', ["%userprofile%\\documents\\extra stuff"]); } 我错过了什么? 请原谅我,如果我问的是什么问题,我有一个devise导向的大脑,更熟悉HTML,CSS和一些Javascript。

将$ ajax转换为PUT请求的提取()

我试图翻译下面的jQuery代码来使用抓取API。 它发出一个PUT请求: function save () { $.ajax('/{{user.username}}', { method: 'PUT', data: { street: $('#street').val(), city: $('#city').val(), state: $('#state').val(), zip: $('#zip').val() }, complete: function () { cancel() location.reload() } }) } 这是抓取API请求: fetch('/{{user.username}}', { method: 'PUT', headers: { 'Content-Type': 'application.json' }, body: JSON.stringify({ street: document.getElementById("street").value, city: document.getElementById("city").value, state: document.getElementById("state").value, zip: document.getElementById("zip").value }) }).then(() => { cancel() […]

socket.io不工作没有错误显示

我正在使用node.js,socket.io和express来处理聊天应用程序。 我正在使用pm2来运行server.js。 在日志上说 0|server | 18-08 12:14:47.128: Server listening on port 3000 在控制台中也没有错误。 这是我的代码。 请忍受我,因为我仍然是一个新手在node.js和socket.io。 每一个帮助将不胜感激。 server.js process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; var app = require('express')(); var https = require('https'); var fs = require('fs'); var ssl_server_key = '/data/web/chat/app/node/server_key.pem'; var ssl_server_crt = '/data/web/chat/app/node/server_crt.pem'; var server = https.createServer({ key: fs.readFileSync(ssl_server_key), cert: fs.readFileSync(ssl_server_crt), NPNProtocols: ['http/2.0', 'spdy', 'http/1.1', 'http/1.0'] }, app); […]

存根内部function?

我想在unit testing时在我的代码中存储一个内部函数,例如: //foobar.js const uuid = require('uuid'); function foo() { console.log('uuid: ' + uuid.v4()); // Lots of timers } exports._foo = foo; function bar() { //Logic… foo(); //Logic… } exports.bar = bar; 而unit testing: // test/foobar.js const chai = require('chai'), expect = chai.expect, proxyquire = require('proxyquire'), sinon = require('sinon'); describe('bar', () => { it('call foo', […]

信号量等同于Node js,variables在并发请求中被修改?

我在过去的一个星期面对这个问题,我只是对此感到困惑。 保持简短的解释问题。 我们有一个内存模型,它存储像预算等值。现在,当一个API的调用它有一个与其相关联的花费。 然后,我们检查内存模型,并将花费添加到现有的花费,然后检查预算,如果超过,我们不会再接受该模型的更多点击。 对于每个调用,我们也使用db,但这是一个asynchronous操作。 一个简短的例子 api.get('/clk/:spent/:id', function(req, res) { checkbudget(spent, id); } checkbudget(spent, id){ var obj = in memory model[id] obj.spent+= spent; obj.spent > obj.budjet // if greater. obj.status = 11 // 11 is the stopped status update db and rebuild model. } 这用于工作正常,但现在与并发请求我们得到错误的花费增加超过预算,并在一段时间后停止。 我们用j米模拟了这个电话,发现了这个。 就我们所能看到的,当状态更新到11时,节点是asynchronous的,许multithreading已经更新了活动的花费。 如何为Node.js提供信号量逻辑,以便可变预算与模型同步 更新 db.addSpend(campaignId, spent, function(err, data) { campaign.spent […]

Sequelize findOne错误:TypeError indexOf undefined

第一次使用一个新的数据库使用sequelize,通过cli完成表创build和迁移(我们有一个传统的数据库模式使用它,没有问题与模型构build匹配现有的表,所以我熟悉的基本用法) 。 当我试图testing一个新的表中的一个查询时,我得到一个typeerror。 系统:Sequelize v4.5.0,Node v8.1.4,npm v5.3.0 首先Stacktrace: TypeError: Cannot read property 'indexOf' of undefined at attributes.map.attr (**PATH**/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1222:27) at Array.map (native) at Object.escapeAttributes (**PATH**/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1207:37) at Object.selectQuery (**PATH**/node_modules/sequelize/lib/dialects/abstract/query-generator.js:979:28) at QueryInterface.select (**PATH**/node_modules/sequelize/lib/query-interface.js:672:27) at Promise.try.then.then.then (**PATH**/node_modules/sequelize/lib/model.js:1539:34) at tryCatcher (**PATH**/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (**PATH**/node_modules/bluebird/js/release/promise.js:512:31) at Promise._settlePromise (**PATH**/node_modules/bluebird/js/release/promise.js:569:18) at Promise._settlePromise0 (**PATH**/node_modules/bluebird/js/release/promise.js:614:10) at Promise._settlePromises (**PATH**/node_modules/bluebird/js/release/promise.js:693:18) at Async._drainQueue (**PATH**/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues (**PATH**/node_modules/bluebird/js/release/async.js:143:10) at […]

函数中的Mongoose查询承诺让代码变得丑陋

我正在创build我的用户API,我想检查用户名是否被使用。 所以我写了一个静态函数 static findByName(name) { const query = User.where({ username: name, }); query.findOne((queryErr, user) => { if (queryErr) { console.log(queryErr); return false; } return user; }); } 当我在signUp中调用它 signup(req, res) { if (!req.body.username || !req.body.password || !req.body.email) { return res.status(400).json({ success: false, message: 'Bad Request' }); } if (!Users.findByName(req.body.username)) { return res.status(409).json({ success: false, message: […]

围绕循环依赖工作

我尝试了第一次sequelize ,我有一点麻烦让我的数据库模型的工作。 sequelize.sync()失败, Error: Circular Dependency Instruction -> Result => Instruction 这是有道理的,因为我的模型是循环的: const Instruction = db.define('instruction', { id: { type: Sequelize.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true, }, /* more keys here */ }); //each instruction results in one or more results const Result = db.define('result', { id: { type: Sequelize.INTEGER, allowNull: false, primaryKey: true, […]