我正在寻找一种优雅的方式来定义可以使用 JSON 数据和表单数据的 api。以下代码段有效,但它并不优雅,并且需要在后端使用各种丑陋的代码。有没有更好的方法来定义这个?
现在什么工作:
paths:
/pets:
post:
consumes:
- application/x-www-form-urlencoded
- application/json
parameters:
- name: nameFormData
in: formData
description: Updated name of the pet
required: false
type: string
- name: nameJSON
in: body
description: Updated name of the pet
required: false
type: string
Run Code Online (Sandbox Code Playgroud)
我希望它如何工作的基本想法:
paths:
/pets:
post:
consumes:
- application/x-www-form-urlencoded
- application/json
parameters:
- name: name
in:
- formData
- body
description: Updated name of the pet
required: true
type: string
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为该in值必须是字符串,而不是数组。
有什么好主意吗?
所以我想编写一个安全规则来禁止写入,如果子节点与之前的节点不同.例如,想象一种情况,您希望节点在创建时只能写入,但之后永远不可写.鉴于此要求,最明显的解决方案似乎是验证新数据是否等于旧数据.
可悲的是,这不起作用:
".write": "data.child('someNode').val() === newData.child('someNode').val()"
Run Code Online (Sandbox Code Playgroud)
也不是一种更复杂的方法,我尝试将对象转换为字符串:
".write": "!data.exists() || (data.child('someNode').val() + '') === (newData.child('someNode').val() + '')"
Run Code Online (Sandbox Code Playgroud)
这种用例有什么方法可以支持吗?
重要说明: someNode 上的值必须是一个对象,不能只是一个字符串或其他原语.在原语的情况下,这些方法中的任何一种都可以正常工作.
Firestore 中的乐观更新似乎没有预期的那么快。我看到更新时间至少为 200 毫秒,在我写入 Firestore 的时间和触发快照侦听器的时间之间。我正在使用 React Native Firebase 库,但我认为问题不太可能源于此。该库只是本机 SDK 的一个薄包装层。我启用了离线持久性,但无论是否有互联网连接,行为都是相同的。
下面的代码片段说明了这个问题:
class App extends React.Component{
componentDidMount(){
db.collection("cities").onSnapshot(function(qsnap) {
this.setState({cities: qsnap.docs.map(s => s.data())})
});
}
addCity(){
db.collection("cities").add({
name: Math.random(),
country: "Japan"
})
}
render(){
//After clicking add city, the new render doesn't happen until
//after a minimum time of 200 ms :(
<View>
<TouchableOpacity onPress={this.addCity}>Add city</TouchableOpacity>
{this.state.cities.map(c => <Text>{c.name}</Text>)}
</View>
}
}
Run Code Online (Sandbox Code Playgroud)
我有什么做错的地方或者可以采取不同的做法吗?我喜欢从数据存储订阅快照并直接从中渲染 UI,然后直接写入数据存储并通过乐观传播让我的 UI 立即更新的模式。
firebase react-native google-cloud-firestore react-native-firebase
我想过滤条件类型数组并具有正确的返回类型。例如,采用以下代码:
const foo = {
type: "foo" as const,
foo: 123
}
const bar = {
type: "bar" as const,
bar: 123
}
const arr = [foo, bar]
const filteredArr = arr.filter(val => val.type === 'foo');
filteredArr.map(val => {
val.foo //Typescript blows up! Property foo does not exist on type
})
Run Code Online (Sandbox Code Playgroud)
我想出的唯一解决方案是投射过滤后的数组:
type Foo = typeof foo;
const filteredArr = arr.filter(val => val.type === 'foo') as Foo[];
Run Code Online (Sandbox Code Playgroud)
但似乎应该有一种打字稿足够聪明的方式来推断数组已被过滤为仅Foo[].
也许用户定义类型保护的一些棘手用法?