简体   繁体   English

似乎无法弄清楚为什么我的 promise 阵列不工作

[英]Can't seem to figure out why my promise array isn't working

I'm working on a project where at one point, I run a series of promises and then push their results (in the.then() portion) to an array.我正在做一个项目,在某个时候,我运行了一系列承诺,然后将它们的结果(在 then() 部分)推送到一个数组。 The issue is that, although the promise itself is providing a result value, nothing is actually being pushed to the array itself.问题是,虽然 promise 本身提供了结果值,但实际上没有任何内容被推送到数组本身。 Below is the create function in my code where I'm using Promise.all() to run a series of processes on an array made of promises called newJoinsPromiseArray .下面是在我的代码中创建 function,我使用Promise.all()在一个由名为newJoinsPromiseArray的承诺组成的数组上运行一系列进程。

 export const create = (req, res, next) => { const newJoinsPromiseArray = []; //This will be an array of the new joins that we are adding for (const worker of req.body.workers){ const newJoinElement = new JoinTable(null, req.body.contact_id, worker.value) //We use value here since it's in the form that multi-select gave us newJoinElement.save().then((result) => { newJoinsPromiseArray.push(JoinTable.findByID(result[0].insertId)) }).catch(err => res.json({message: err})) } Promise.all(newJoinsPromiseArray).then((values) => {console.log(values)}) }

So whenever I console.log(newJoinsPromiseArray), it just prints [] .因此,每当我使用 console.log(newJoinsPromiseArray) 时,它只会打印[] I've tried this also by just running the JoinTable.findByID function on each element then pushing what it returns to the array, although I think that's the same thing.我也通过在每个元素上运行 JoinTable.findByID function 然后将它返回的内容推送到数组来尝试此操作,尽管我认为这是同一回事。

Below are my functions for save and findByID:以下是我的保存和查找功能:

 save(){ /* The purpose of this function is to save a new element to the database. */ return db.execute(`INSERT INTO workercontacts (contact_id, worker_id) VALUES(?, ?)`, [this.contact_id, this.worker_id]); }

 static findByID(element_id){ // Will give us a specific element based on the id return db.execute('SELECT * FROM workerContacts WHERE workerContacts.id =?', [element_id]); }

I'm using a MySQL database which I don't think has anything to do with this issue but I thought I'd add that information just in case.我正在使用 MySQL 数据库,我认为它与此问题没有任何关系,但我想我会添加该信息以防万一。

Edit:编辑:

Adding on to this, this was my new attempt at making this work, which it still didn't除此之外,这是我进行这项工作的新尝试,但仍然没有成功

 export const create = async (req, res, next) => { const newJoinsPromiseArray = []; //This will be an array of the new joins that we are adding for (const worker of req.body.workers){ const newJoinElement = new JoinTable(null, req.body.contact_id, worker.value) //We use value here since it's in the form that multi-select gave us try{ const savedResult = await newJoinElement.save(); const joinElementByID = await JoinTable.findByID(savedResult[0].insertId); newJoinsPromiseArray.concat(joinElementByID); } catch (e){ res.json({message: e}) } } console.log(newJoinsPromiseArray) }

I'm still lost unfortunately but thank you for all of your help thusfar.不幸的是,我仍然迷路了,但感谢您迄今为止的所有帮助。

You should be pushing the Promise s into the array.您应该将Promise推入数组。

newJoinsPromiseArray.push(newJoinElement.save().then(result => JoinTable.findByID(result[0].insertId));

Alternatively, you could make the function async and use await on each of the save operations in the loop.或者,您可以使 function async并在循环中的每个保存操作上使用await

I figured it out thanks to @Unmitigated: I just changed my create function to be this:感谢@Unmitigated,我弄明白了:我刚刚将创建的 function 更改为:

 export const create = (req, res, next) => { const newJoinsPromiseArray = []; for (const worker of req.body.workers){ const newJoinElement = new JoinTable(null, req.body.contact_id, worker.value) //We use value here since it's in the form that multi-select gave us newJoinsPromiseArray.push(newJoinElement.save().then(result => JoinTable.findByID(result[0].insertId))) } Promise.all(newJoinsPromiseArray).then((values) => res.json(values.map(val => val[0]).flat())) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 似乎无法弄清楚该脚本为何不起作用 - Can't seem to figure out why the script isn't working 似乎无法弄清楚为什么我的JavaScript无法在IE和Chrome中运行 - Can't seem to figure out why my javascript isn't working in IE and Chrome 我的javascript函数似乎无法正常工作。 我不知道为什么 - My javascript function doesn't seem to be working. I can't figure out why 无法弄清楚为什么JS插件无法正常工作 - Can't figure out why JS plugin isn't working 无法弄清楚为什么它不起作用了 - Can't figure out why it isn't working, anymore 无法弄清楚为什么我的change(...)事件处理程序在这种情况下不起作用 - Can't figure out why my change(…) event handler isn't working in this scenario 我不知道为什么我的石头,剪刀布游戏中的得分不起作用 - I can't figure out why the scoring isn't working in my rock, paper, scissors game 无法弄清楚为什么javascript无法在我的php文件中工作 - Can't figure out why javascript isn't working in my php file 我无法弄清楚为什么我添加事件处理程序的简单尝试无效 - I can't figure out why my simple attempt at adding an event handler isn't working 经典的元音练习。 我不知道为什么我的for循环无法正常工作 - classic vowel exercise. I can't figure out why my for loop isn't working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM