小编use*_*719的帖子

findOrCreate 与包含 - sequelize.js

我想创建一个锦标赛(如果不存在)和一个与锦标赛关联的匹配(如果不存在)。

let [match, created] = await Match.findOrCreate( {
  where: {scoreHome: 97, Tournament: {name: "USA South Men's Basketball"}}, 
  include: [Tournament]
})
Run Code Online (Sandbox Code Playgroud)

如果锦标赛“美国南部男子篮球”已经在数据库中(假设 ID 为 1),则应使用 TournamentId: 1 创建比赛。如果数据库中已经有比赛{scoreHome: 97 and TournamentId: 1},则不应创建比赛.

  let Match = sequelize.define('Match', {
    scoreHome: DataTypes.INTEGER,
    //...
  })

  Match.associate = models => {
    Match.belongsTo(models.Tournament)
  }
Run Code Online (Sandbox Code Playgroud)

编辑 这是错误:

[错误:无效值 [对象对象]]

这工作正常

let match = await Match.create({
    scoreHome: 97, Tournament: {name: "USA South Men's Basketball"}
  },{include: [Tournament]}
})
Run Code Online (Sandbox Code Playgroud)

javascript sequelize.js

7
推荐指数
1
解决办法
912
查看次数

与Emberjs的Strongloop

Ember Data的REST适配器以这种格式接受来自服务器的JSON:

摘自文档:http://guides.emberjs.com/v1.10.0/models/the-rest-adapter/

{
  "post": {
    "id": 1,
    "title": "Node is not omakase",
    "comments": [1, 2, 3]
  },

  "comments": [{
    "id": 1,
    "body": "But is it _lightweight_ omakase?"
  },
  {
    "id": 2,
    "body": "I for one welcome our new omakase overlords"
  },
  {
    "id": 3,
    "body": "Put me on the fast track to a delicious dinner"
  }]
}
Run Code Online (Sandbox Code Playgroud)

是否有可能从strongloop返回这种JSON格式?

ember.js strongloop loopbackjs

6
推荐指数
1
解决办法
763
查看次数

Ember CLI无法找到部分

我正在做一个插件.我尝试使用偏.在我的addon/templates/components/form.hbs中我得到了{{partial "pane"}}.我_pane.hbs在插件/模板文件夹,但烬不能找到它:

未捕获错误:断言失败:无法找到名称为"窗格"的部分.

我也尝试将_pane.hbs放在插件/模板/组件中,但没有运气.我应该把它放在哪里被灰烬捡起来?

ember.js ember-cli

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

.find 不是cheerio 对象上的函数

  let playersCell = `
    <td class="foo" colspan="2">
      <a href="example.com">
        <span class="bold">John Beluga</span>
         - Sarah Jay.
       </a>
    </td>
    `

let players = cheerio.load(playersCell)
players.find('a').html()
Run Code Online (Sandbox Code Playgroud)

我尝试将 html 字符串加载到 Cheerio.js 中并找到一个a标签,但我得到了

[类型错误:players.find 不是函数]

Console.log显示为players

在此输入图像描述

javascript node.js cheerio

5
推荐指数
2
解决办法
4502
查看次数

在嵌套的 for ... of 循环中等待

  async traverse(url) {
    const ts = new TournamentScraper()
    const ms = new MatchScraper()
    const results = []
    const tournaments = await ts.run(url)
    for(let href of tournaments.map(t => t.href)){
      let matches = await ms.run(href)
      let pages = ms.getPages()
      let seasons = ms.getSeasons()
      //console.log(pages)
      //console.log(seasons)
      results.push(matches)
      for(let href of pages) {
        //console.log(href)
        matches = await ms.run(href)
        //console.log(matches)
        results.push(matches)
      }
    }

    return results
  }
Run Code Online (Sandbox Code Playgroud)

TournamentScraper 返回一个对象数组,通常如下所示:

{name: 'Foo', href: 'www.example.org/tournaments/foo/'}
Run Code Online (Sandbox Code Playgroud)

该链接指向锦标赛上赛季的首页。此页面包含其他季节的链接和分页器(如果有)。

MatchScraper 的运行返回一些数据,并设置实例的 dom 属性。getPages()getSeasons()使用此属性,每个属性都返回一个链接数组。

结果的问题仅包含第一批匹配项。我可以在控制台日志中看到第二页的匹配项,但返回时它们不在结果数组中traverse

我发现这条 …

javascript async-await

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

用ava测试承诺

我试着测试这个课程

class Scraper {
  async run() {
    return await nightmare
      .goto(this.url)
      .wait('...')
      .evaluate(()=>{...})
      .end
  }
}
Run Code Online (Sandbox Code Playgroud)

我的测试看起来像这样:

test('Scraper test', t => {
  new Scraper().run().then(() => {
    t.is('test', 'test')
  })
})
Run Code Online (Sandbox Code Playgroud)

测试失败:

测试完成后不运行任何断言

编辑

github上的存储库:https://github.com/epyx25/test

测试文件:https://github.com/epyx25/test/blob/master/src/test/scraper/testScraper.test.js#L12

testing node.js promise nightmare ava

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