小编mCY*_*mCY的帖子

在哪里破坏 knex 连接

我正在使用knexpg

我有一个类似于以下的项目。

数据库客户端.js

const dbClient = require('knex')({
  client: 'pg',
  connection: {
    host: '127.0.0.1',
    user: 'user',
    password: 'password',
    database: 'staging',
    port: '5431'
  }
})

module.exports = dbClient
Run Code Online (Sandbox Code Playgroud)

库.js

const knex = require('./dbClient.js')

async function doThis(email) {
  const last = await knex('users').where({email}).first('last_name').then(res => res.last_name)
  // knex.destroy()
  return last
}

async function doThat(email) {
  const first = await knex('users').where({email}).first('first_name').then(res => res.first_name)
  // knex.destroy()
  return first
}

module.exports = {
  doThat,
  doThis
}
Run Code Online (Sandbox Code Playgroud)

测试01.js

const {doThis, doThat} = require('./libs.js');

(async …
Run Code Online (Sandbox Code Playgroud)

node.js knex.js

10
推荐指数
3
解决办法
2万
查看次数

赛普拉斯将响应正文中的内容保存为别名或变量

cy.request用来创建一个新用户。我需要获取userID并使用它来组装一个 url。

例如:

function createUser () {
  cy.request({
    method: 'GET',
    url: `/human/sign_in`
  }).then(response => {
    const $ = cheerio.load(response.body)
    const token = $('css to get the token')
    cy.request({
      method: 'POST',
      url: `/signups/brands`,
      form: true,
      body: {
        'authenticity_token': token,
        'name': 'some name',
        'email': 'some email'
      }
    })
  }).then(response => {
    const $ = cheerio.load(response.body)
    const userID = $('css to get userID') // here's the userID
  })
}
Run Code Online (Sandbox Code Playgroud)

如何返回 this userID,以及如何在以下代码中引用它?

describe('some feature', () => {
  it('should …
Run Code Online (Sandbox Code Playgroud)

cypress

5
推荐指数
1
解决办法
6311
查看次数

使用cheerio获取属性列表

['http://www.1.com', 'http://www.2.com', 'http://www.3.com']我想从以下字符串中获取。

const cheerio = require('cheerio')

const htmlStr = `
<div>

  <div class="item">
    <a href="http://www.1.com"></a>
  </div>

  <div class="item">
    <a href="http://www.2.com"></a>
  </div>

  <div class="item">
    <a href="http://www.3.com"></a>
  </div>

</div>
`

const $ = cheerio.load(htmlStr)
Run Code Online (Sandbox Code Playgroud)

最初,我认为$(div.item a)会返回一个元素数组。所以我尝试了:

const urls = $('div.item a').map(x => x.attr('href'))
Run Code Online (Sandbox Code Playgroud)

它失败了。

似乎$('div.item a')返回一个object.

怎么做?

谢谢!

node.js cheerio

5
推荐指数
1
解决办法
1万
查看次数

摩卡异步/等待测试失败并超时

我想用 测试异步代码Mocha

我按照本教程testing-promises-with-mocha进行操作。最后,它说最好的方法是 async/await。

以下是我的代码,我打算将 setTimeout 设置得比 Mocha 默认值长。

describe('features', () => {
  it('assertion success', async() => {
    const resolvingPromise = new Promise( resolve => {
      setTimeout(() => {
        resolve('promise resolved')
      }, 3000)
    })

    const result = await resolvingPromise
    expect(result).to.equal('promise resolved')
  })
})
Run Code Online (Sandbox Code Playgroud)

摩卡给我错误如下:

Error: Timeout of 2000ms exceeded. For async tests and hooks, 
ensure "done()" is called; if returning a Promise, ensure it resolves...
Run Code Online (Sandbox Code Playgroud)

如何解决该错误?简单设置mocha --timeout 10000更长?

谢谢你的时间!

Mocha: 5.2.0
Chai: 4.2.0
Run Code Online (Sandbox Code Playgroud)

mocha.js async-await

5
推荐指数
1
解决办法
4935
查看次数

在材质UI的&lt;ExpansionPanelDetails&gt;中保留新行

我得到一个扩展的面板,如下所示:

  <ExpansionPanel>
    <ExpansionPanelSummary>this is a title</ExpansionPanelSummary>
    <ExpansionPanelDetails>
      <Typography>line 1</Typography>
      <Typography>line 2</Typography>
      <Typography>line 3</Typography>
    </ExpansionPanelDetails>
  </ExpansionPanel>
Run Code Online (Sandbox Code Playgroud)

我需要ExpansionPanelDetails保持多行内容:

line 1
line 2
line 3
Run Code Online (Sandbox Code Playgroud)

但实际上是:

line 1line 2line 3
Run Code Online (Sandbox Code Playgroud)

我试着总结<Typography><div>和添加<br />。都行不通。

有任何想法吗?

谢谢!

"@material-ui/core": "^3.7.0"

reactjs material-ui

4
推荐指数
1
解决办法
508
查看次数

puppeteer 如何在父元素中查找元素

使用cypress我可以找到子元素within元素,如下所示:

cy.get('div#Login_form).within(() => {
  cy.get('input[name="human[email]"]').type('John')
  cy.get('input[name="human[password]"]').type('123456')
})
Run Code Online (Sandbox Code Playgroud)

Puppeteerfor有什么等价物within()吗?

谢谢!

node.js puppeteer

4
推荐指数
1
解决办法
3750
查看次数

温斯顿如何修改时间戳、级别、消息等的输出顺序

我用于winston记录,这是我的代码:

const { createLogger, format, transports } = require('winston')

const logger = createLogger({
  level: 'info',
  format: format.combine(
    format.timestamp({format: 'YYYY-MM-DDTHH:mm:ss'}),
    format.json()
  ),
  transports: [new transports.File({filename: 'logs/new_combined.log'})]
})

logger.info('haha')
Run Code Online (Sandbox Code Playgroud)

输出是:

{"message":"haha","level":"info","timestamp":"2019-01-03T11:13:32"}
Run Code Online (Sandbox Code Playgroud)

我想调整输出顺序以使其timestamp提前,然后level,然后message,如下所示:

{"timestamp":"2019-01-03T11:13:32","level":"info","message":"haha"}
Run Code Online (Sandbox Code Playgroud)

怎么做?

谢谢!

node.js winston

3
推荐指数
1
解决办法
2084
查看次数

puppeteer querySelector 不是有效的选择器

我有一个代码如下:

page.click('div.button-table div:contains(Who) div.square-button:nth-child(1)')
Run Code Online (Sandbox Code Playgroud)

当 puppeteer 运行此代码时,它会引发错误:

简短的

Failed to execute 'querySelector' on 'Document': 'div.button-table div:contains(Who) div.square-button:nth-child(1)' is not a valid selector.

满的

 Error: Evaluation failed: DOMException: Failed to execute 'querySelector' on 'Document': 'div.button-table div:contains(Who) div.square-button:nth-child(1)' is not a valid selector.
at __puppeteer_evaluation_script__:1:33
  at ExecutionContext.evaluateHandle (node_modules/puppeteer/lib/ExecutionContext.js:124:13)
  at <anonymous>
-- ASYNC --
  at ExecutionContext.<anonymous> (node_modules/puppeteer/lib/helper.js:144:27)
  at ElementHandle.$ (node_modules/puppeteer/lib/ExecutionContext.js:529:50)
  at ElementHandle.<anonymous> (node_modules/puppeteer/lib/helper.js:145:23)
  at Frame.$ (node_modules/puppeteer/lib/FrameManager.js:456:34)
  at <anonymous>
-- ASYNC --
  at Frame.<anonymous> (node_modules/puppeteer/lib/helper.js:144:27)
  at Frame.click (node_modules/puppeteer/lib/FrameManager.js:735:31)
  at Frame.<anonymous> (node_modules/puppeteer/lib/helper.js:145:23)
  at Page.click (node_modules/puppeteer/lib/Page.js:973:29) …
Run Code Online (Sandbox Code Playgroud)

javascript webautomation node.js puppeteer

3
推荐指数
1
解决办法
6113
查看次数

ApolloClient不是构造函数(带有Node.js的Apollo-client)

我没有任何UI框架。只是需要查询GraphQL的简单Node.js脚本。

代码:

const ApolloClient = require('apollo-client')
const client = new ApolloClient()
Run Code Online (Sandbox Code Playgroud)

错误信息:

TypeError: ApolloClient is not a constructor
Run Code Online (Sandbox Code Playgroud)

Package.json:

{
  ...

  "dependencies": {
    "apollo-client": "^2.4.13",
    "graphql": "^14.1.1",
    "graphql-tag": "^2.10.1"
  },
}
Run Code Online (Sandbox Code Playgroud)

节点: v8.9.4

我用Google搜索了一会儿,这个人主要是因为 ApolloClient is no longer in react-apollo. You have to import it from 'apollo-client'

而我从进口apollo-clientconst ApolloClient = require('apollo-client')

有任何想法吗?谢谢!

node.js graphql

3
推荐指数
2
解决办法
831
查看次数