小编ree*_*ces的帖子

如何覆盖 Typescript 中交集类型的属性?

假设我有以下类型:

type BaseAnimal = {
  species: string
  owner: boolean
}

type Cat = BaseAnimal & {
  species: 'cat'
  hasTail: boolean
}

type Dog = BaseAnimal & {
  species: 'dog'
  likesWalks: boolean
}

type Animal = Cat | Dog
Run Code Online (Sandbox Code Playgroud)

我想创建一个名为 的类型,除了AnimalParams属性是一个字符串之外,它与其他类型相同Animal owner

以下任何一项我都做不到。

// This seems to keep the owner property from Animal instead of overwriting
// So it raises an error if I try to specify owner as a string
type AnimalParams = Animal & …
Run Code Online (Sandbox Code Playgroud)

types typescript .d.ts

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

Minitest中“def setup”和“setup do”的区别?

调用def setupsetup doRails Minitests之间有什么区别吗?我一直在使用def setup这个,但我突然发现我setup的特定测试文件没有被调用。当我将其更改为 时setup do,它突然再次起作用(没有更改任何其他内容)。但我觉得这很奇怪,def setup如果可能的话,我宁愿坚持所有事情,以保持一致性。任何建议表示赞赏。

require 'test_helper'
require_relative '../../helpers/user_helper'

class FooTest < ActiveSupport::TestCase
  include UserHelper

  # This method doesn't get called as-is.
  # But it does get called if I change the below to `setup do`.
  def setup
    # create_three_users is a UserHelper method.
    create_three_users
    @test_user = User.first
  end


  test 'should abc' do
    # Trying to call @test_user here returned nil.
  end
end
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails minitest

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

React-i18n Trans 组件包含 HTML 标签的翻译不起作用

我正在尝试使用react-i18next包含一些 HTML 标签的翻译 json,如下所示:

// en.json
{ "welcome": "Welcome to our site, <strong>{{name}}</strong>" }
Run Code Online (Sandbox Code Playgroud)

在我的 React 组件中,我希望字符串像这样呈现。

欢迎来到我们的网站,约翰

使用该useTranslation函数通常按字面意思打印字符串,而不将其解释为 HTML,例如Welcome to our site, <strong>John</strong>.

import React from 'react'
import { useTranslation } from 'react-i18next'

const App = () => {
  const { t } = useTranslation()

  return(
    <div>{t('welcome', { name: 'John' })}</div>
  )
}
Run Code Online (Sandbox Code Playgroud)

我将其替换为dangerouslySetInnerHTML并且它已正确呈现。

<div dangerouslySetInnerHTML={{ __html: t('welcome', { name: 'John' }) }}></div>
Run Code Online (Sandbox Code Playgroud)

但是,如果可能的话,我想避免使用dangerouslySetInnerHTML。我在文档中读到,您可以使用称为 Trans 组件的东西来进行 HTML 标签的翻译。 …

i18next reactjs react-i18next

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

如何通过first_or_initialize而不是find_or_initialize_by(Rails)使用joins方法?

有没有办法find_or_initialize_by使用该joins方法重写下面当前使用的过程?

对于上下文-我有users(员工)谁attendances在系统中记录他们(a user有很多attendances,并且一条attendance记录属于a user)。

Attendance.find_or_initialize_by(
  user: User.find_by(name: 'Bob'),
  date: Time.zone.today
)
.update(...) # Update some columns after this
Run Code Online (Sandbox Code Playgroud)

我正在尝试.joins像这样重写它:

Attendance.joins(:user)
  .where(users: {name: 'Bob'}, date: Time.zone.today)
  .first_or_initialize
  .update(...) # Update some columns after this
Run Code Online (Sandbox Code Playgroud)

我的测试返回了混合结果:

  • 只需要updated通过记录的测试用例
  • attendance尚不存在该记录的测试用例(例如,当我不得不时initialize)失败

错误消息是 ActiveRecord::RecordInvalid: Validation failed: User must exist。但是我很困惑,因为用户实际上存在-如果我使用进行调试byebug,则用户在那里。

ruby join ruby-on-rails inner-join

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

在 Vue 项目中使 axios 全局化不起作用

到目前为止,我一直在每个 Vue 组件中导入 axios,我想在其中发出 HTTP 请求,就像这样。

<script lang="ts">
import axios from 'axios';

@Component
export default class ExamplePage extends Vue {
  created(): void {
    axios.post(some_path);
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,现在我想为所有 axios 请求定义一个全局拦截器,基本上是为了捕获401 unauthorized来自后端服务器(Rails)的所有响应并注销用户。到目前为止,我的理解是您必须将 axios 实例化一次并在任何地方使用它,而不是在每个文件中导入和使用不同的实例。

我已经提到了thisthis,并尝试了以下内容。

// application.js

import '../assets/sass/base.sass';
import App from '../app';
import Vue from 'vue';
import VueRouter from 'vue-router';
import axios from 'axios';
import router from '../routes';

Vue.use(VueRouter);

document.addEventListener('DOMContentLoaded', () => {
  new Vue({
    el: '#application',
    router,
    render: (h) => h(App),
  });
});

axios.interceptors.response.use( …
Run Code Online (Sandbox Code Playgroud)

javascript typescript vue.js axios

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

在本地调用函数时,AWS SAM CLI 无法访问 Dynamo DB

我正在使用aws-sam-cli构建一个 AWS lambda 。在函数中,我想访问某个 DynamoDB 表。我的问题是,当我使用以下sam local invoke命令在本地调用该函数时,该函数会返回此错误:ResourceNotFoundException: Requested resource not found

const axios = require('axios')
const AWS = require('aws-sdk')
AWS.config.update({region: <MY REGION>})
const dynamo = new AWS.DynamoDB.DocumentClient()

exports.handler = async (event) => {
  const scanParams = {
    TableName: 'example-table'
  }
  const scanResult = await dynamo.scan(scanParams).promise().catch((error) => {
    console.log(`Scan error: ${error}`)
    // => Scan error: ResourceNotFoundException: Requested resource not found
  })

  console.log(scanResult)
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我真的将sam deploy它发送到 AWS 并在实际的 Lambda 控制台中对其进行测试,它会正确记录表信息。

{
  Items: <TABLE ITEMS>, …
Run Code Online (Sandbox Code Playgroud)

amazon-web-services amazon-dynamodb aws-lambda aws-sam-cli

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

Rails 加载错误 - bcrypt 的库版本不兼容

我的rails s电脑rails c突然开始拒绝启动,并出现以下错误。我不记得更新或删除过任何宝石。非常感谢任何帮助解决这种情况的帮助。

incompatible library version - /Users/[me]/projects/[app]/vendor/bundle/ruby/2.6.0/gems/bcrypt-3.1.12/lib/bcrypt_ext.bundle (LoadError)

bcrypt不是我们项目中明确需要的 gem,但它包含在其中,Gemfile.lock因为它是一个依赖项:bcrypt (3.1.12)

到目前为止我已经尝试过这些,但我得到了同样的错误:

  1. 在我的系统中卸载和安装bcrypt(根据此建议
gem uninstall bcrypt -v 3.1.12

You have requested to uninstall the gem:
        bcrypt-3.1.12

sorcery-0.14.0 depends on bcrypt (~> 3.1)
If you remove this gem, these dependencies will not be met.
Continue with Uninstall? [yN]  y
Successfully uninstalled bcrypt-3.1.12
Run Code Online (Sandbox Code Playgroud)
gem install bcrypt -v 3.1.12
Successfully installed bcrypt-3.1.12
Run Code Online (Sandbox Code Playgroud)
  1. gem pristine --all

以下是检查 gem 版本时得到的结果:

  1. gem info …

ruby rubygems ruby-on-rails bcrypt

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