编辑:为什么这不是重复的:因为Cypress,只是读取而不是将所有内容标记为重复。
编辑 2:另外,请参阅答案以更好地理解通常的异步for循环问题与此问题之间的差异。
我正在编写 cypress 测试,我想创建一个 cypress 命令,用用户列表填充我的数据库。我希望创建循环在移动到下一个用户之前等待每个用户被创建(因为我希望以特定顺序完成)。
现在,我的循环如下所示:
Cypress.Commands.add("populateDb", (users) => {
var createdItems = []
for (const user of users) {
cy.createUser(user, 'passe').then(response => {
createdUsers.unshift(response.body.user)
})
}
return createdItems
})
Run Code Online (Sandbox Code Playgroud)
当然,这个循环在移动到下一个用户之前不会等待每个用户被创建(我想要“顺序处理”,而不是“并行然后等待所有承诺解决”)
我在这里阅读了有关异步 for 循环的答案:
但我似乎找不到我想要的东西,主要是因为 cypress 不允许我将我的函数声明为异步,如下所示:
Cypress.Commands.add("populateDb", async (users) => {
//Some code
})
Run Code Online (Sandbox Code Playgroud)
如果我不声明它,async我将无法使用await.
是不是有一些get()方法之王只是同步等待 Promise 解决?
我在 vuex 商店中使用 Vue.js。我调用一个 API 方法来验证一个项目,该方法返回一个错误数组和一个警告数组。
我的 vuex 动作:
export function validateitemReview ({ commit, dispatch, state }, { reviewId, type, itemreviewData }) {
console.log('*** validateItemReview() called')
return api.post(`/clients/districts/reviews/${reviewId}/${type}/validate`, itemreviewData).then(response => {
console.log('response.data :')
console.log(response.data)
commit('setItemReviewsErrors', 'teststring')
})
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我还没有对响应做太多处理。setItemReviewsErrorsvuex store 中被调用的mutation:
export const setItemReviewsErrors = (state, data) => {
console.log('*** setItemReviewsErrors() called, data:')
console.log(data)
}
Run Code Online (Sandbox Code Playgroud)
我的问题来了,我的控制台输出如下:
*** validateItemReview() called
response.data :
{
fatal_errors: []
warnings: []
__proto__: Object
}
*** setItemReviewsErrors() called, data:
teststring
Run Code Online (Sandbox Code Playgroud)
直接跟着这个错误:
Uncaught (in promise) …Run Code Online (Sandbox Code Playgroud) First of all, I found no duplicate of this, since the problem is not trying to avoid the break inside a row (this already works well) and I am not allowed to repeat headers on the next page.
Misc. threads that did not help with the problem but are related :
使用Java 8。
我有一个Logger类,它在需要记录某些内容时会调用API。我意识到,如果API配置不正确,或者API无法解决问题,我的日志操作将花费大量时间。
同步日志记录的示例:
public void debug(String message) {
MDC.clear();
MDC.put(SOME_KEY, "SOME_VALUE");
super.debug(message);
MDC.clear();
}
Run Code Online (Sandbox Code Playgroud)
我能够查明问题出在这里,因为如果我仅评论所有内容并停止记录或执行任何操作,那么一切将以应有的速度运行:
public void debug(String message) {
// MDC.clear();
// MDC.put(SOME_KEY, "SOME_VALUE");
// super.debug(message);
// MDC.clear();
}
Run Code Online (Sandbox Code Playgroud)
所以我想将其异步调用,因为我不在乎是否同步记录它:
public void debug(String message) {
CompletableFuture.runAsync(() -> {
MDC.clear();
MDC.put(SOME_KEY, "SOME_VALUE");
super.debug(message);
MDC.clear();
});
}
Run Code Online (Sandbox Code Playgroud)
但是,对于我的主应用程序而言,异步调用与同步调用一样糟糕,在性能方面也是明智的。我想念什么?
假设我有以下数组:
let path = ['foo','bar']
Run Code Online (Sandbox Code Playgroud)
我有这个项目:
let item = {
faa: 'whatever',
foo: {
bar: 'hello there', //general kenobi
bor: 'nope'
}
}
Run Code Online (Sandbox Code Playgroud)
我想使用以下内容访问"你好":
item[path] or item.path or item[path.join(".")]
Run Code Online (Sandbox Code Playgroud)
你明白了,这是可行的,如果是的话,怎么样?(当然这个问题写的不起作用)
所以,我对vue.js很新,这很可能是我的愚蠢错误,但唉,谷歌今天似乎不是我的朋友,因为我已经搜索了2个多小时,发现没有.
尝试加载组件时出现问题.该组件嵌套在其他组件中,并且路由器应该加载它.我有其他组件加载这种方式(使用<router-view></router-view>和路由器)没有任何问题.与此组件唯一不同的是它在其根目录下嵌套了3个级别,而在我的所有其他组件中,我最深的是2个级别.
当我尝试加载此组件时,我收到2警告和错误:
警告1:
[vue-router] Failed to resolve async component render: TypeError: Cannot read property '$createElement' of undefined
Run Code Online (Sandbox Code Playgroud)
警告2:
[vue-router] uncaught error during route navigation
Run Code Online (Sandbox Code Playgroud)
错误:
TypeError: Cannot read property '$createElement' of undefined
at render (eval at ./node_modules/vue-loader/lib/template-compiler/index.js?{"id":"data-v-d2e33d82","hasScoped":false,"optionsId":"0","buble":{"transforms":{}}}!./node_modules/vue-loader/lib/selector.js?type=template&index=0!./src/pages/config-pages/sms/smsConfigDetails.vue (app.js:1656), <anonymous>:5:16)
at eval (vue-router.esm.js?fe87:1774)
at eval (vue-router.esm.js?fe87:1801)
at Array.map (<anonymous>)
at eval (vue-router.esm.js?fe87:1801)
at Array.map (<anonymous>)
at flatMapComponents (vue-router.esm.js?fe87:1800)
at eval (vue-router.esm.js?fe87:1736)
at iterator (vue-router.esm.js?fe87:1943)
at step (vue-router.esm.js?fe87:1717)
Run Code Online (Sandbox Code Playgroud)
我的路由看起来像这样; 我的beforeEnter钩子应该被调用,无法加载的组件是smsConfigDetails:
import allConfigs from 'pages/config-pages/allConfigs'
import …Run Code Online (Sandbox Code Playgroud) 我从以下形状的API中获取了一个项目:
data : {
"56": { ... }, //item1
"57": { ... }, //item2
"58": { ... }, //item3
}
Run Code Online (Sandbox Code Playgroud)
我想要一个像这样的Object数组:
[
{ ... }, //item1 with key "56"
{ ... }, //item2 with key "57"
{ ... } //item3 with key "58"
]
Run Code Online (Sandbox Code Playgroud)
这当然可以用Object.values(data).但由于Javascript不保证对象属性的顺序,我想找到一种方法来使用属性的键对结果进行排序,并保证我将得到的对象数组的顺序.这可行吗?
编辑:根据评论中的建议,我可以使用Object.keys(),对结果进行排序,然后使用它来构建我的数组,但我想知道是否有更直接和优雅的方法来实现这一点.
javascript ×5
asynchronous ×2
vue.js ×2
vuejs2 ×2
css ×1
css-print ×1
cypress ×1
ecmascript-6 ×1
html ×1
java ×1
performance ×1
printing ×1
quasar ×1
vue-router ×1
vuex ×1