gpt4 book ai didi

node.js - 解析 promise 数组 node.js

转载 作者:行者123 更新时间:2023-11-29 13:51:14 33 4
gpt4 key购买 nike

我是 promises 的新手,我正在尝试在 Node.js 中将 RSVP promises 与 PostgreSQL 结合使用,但我很可能做错了。任何有关如何解决该问题或如何改进代码的建议都将受到赞赏。

我试图实现的是:接收数据后 - 处理数据以创建 SQL 更新查询,并在它们准备就绪时 - 执行它们。此处的数据是用户 ID 数组。

什么不起作用:我得到了一组无法解析的 promise 数组,我尝试像这样解析数组:

var promise4all = RSVP.all(
updateQueries.map((innerPromiseArray) => {
return RSVP.all(innerPromiseArray);
})
);

promise4all.then((promiseGroupResult) => {
// doesn't get here
});

但它也不起作用。

代码:

1) 函数'update'接收数据并调用函数'promiseQuery'处理数据:

const RSVP = require('rsvp');

let db;

const update = (data) => {
let users = {
items: data.user, // data to be updated in db - array of user ids
item_type: 1,
id: data.department
}

let updateQueries = [];

// adding query promises to updateQueries
updateQueries.push(promiseQuery(users.id, users.item_type, users.items));

RSVP.all(updateQueries).then((results) => {

/* here 'results' looks like that:
[ [ { query: 'INSERT INTO link_to_department (item_type, department, item) VALUES ($item_type, $department, $item)',
values: [Object] },
{ query: 'DELETE FROM link_to_department WHERE department = $(department) AND item_type = $(item_type) AND item=$(item)',
values: [Object] } ] ]

db update below fails with '[Empty or undefined query.]'*/

db.tx((trx) => {
let sqlUpdates = [];

results.forEach((query) => {
sqlUpdates.push(trx.none(query.query, query.values))
})

return trx.batch(sqlUpdates);
}).then(() => {
res.sendStatus(204);
}).catch((err) => {
console.log('error', err.message);
// handle errors
});
});
};

2) 函数 'promiseQuery' 处理数据(它比较接收到的数据和数据库中的数据以用新数据更新数据库):

const promiseQuery = (department_id, item_type, items) => {
return new RSVP.Promise((resolve, reject) => {
db.query('SELECT item FROM link_to_department WHERE department=' + department_id + ' AND item_type=' + item_type)
.then((db_items) => {
let promises = [];

let itemsToBeRemoved = [];
let itemsToBeAdded = [];

/* here we have array of user ids we received: 'items'
and array of user ids from db: 'db_items' */

// populating 'itemsToBeAdded' and 'itemsToBeRemoved' with user ids that need to added or removed:
populateUpdateArray(items, db_items, itemsToBeAdded);
populateUpdateArray(db_items, items, itemsToBeRemoved);

let insert_query = 'INSERT INTO link_to_department (item_type, department, item) VALUES ($item_type, $department, $item)'
let delete_query = 'DELETE FROM link_to_department WHERE department = $(department) AND item_type = $(item_type) AND item=$(item)';

// creating update sql queries
populateUpdateQuery(insert_query, itemsToBeAdded, department_id, item_type, promises);
populateUpdateQuery(delete_query, itemsToBeRemoved, department_id, item_type, promises);

RSVP.all(promises).then((results) => {
/* here 'results' looks like this:
[ { query: 'INSERT INTO link_to_department (item_type, department, item) VALUES ($item_type, $department, $item)',
values: { item_type: 19, department: 1, item: '1' } },
{ query: 'DELETE FROM link_to_department WHERE department = $(department) AND item_type = $(item_type) AND item=$(item)',
values: { item_type: 19, department: 1, item: 1 } }] */

return resolve(results);
});

}).catch(() => {
reject();
})
});
};

3) “populateUpdateArray”函数填充需要更新的用户 ID 数组(基本上,接收到的用户 ID 应该替换数据库中的 ID——为此我们检查我们收到的哪些 ID 不在数据库中,哪些 ID 在数据库中不在收到的 ID 中):

const populateUpdateArray = (array_0, array_1, updateArray) => {
array_0.forEach((item) => {
if (array_1.indexOf(item) === -1) {
updateArray.push(item);
}
});
};

4) “populateUpdateQuery”函数返回 sql 更新查询:

const populateUpdateQuery = (query, id_array, department_id, item_type, promises) => {
return new RSVP.Promise((resolve, reject) => {
id_array.forEach((item) => {
let values = {
item_type: item_type,
department: department_id,
item: item
};

promises.push({query, values});
});

resolve(promises);
});
};

谢谢!

编辑: 我将代码更改为只有一个数据库连接,并稍微简化了代码。我没有收到任何错误,但仍然没有执行查询。我想我在这里遗漏了一些基本的东西:

const update = (data) => {
let users = {
items: data.user,
item_type: 1,
id: data.department
}

db.tx((tx) => {
let updateQueries = [];

updateQueries.push(promiseQuery(department.id, users.item_type, users.items, tx));

RSVP.all(updateQueries).then((results) => {
// results is array of array, so i flatten it
let sqlUpdates = results.reduce((a, b) => { return a.concat(b); }, []);

/* sqlUpdates here:
[ Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined } ]
*/

return tx.batch(sqlUpdates);
});
}).then(() => {
res.sendStatus(204);
}).catch((err) => {
console.log('error', err.message);
});
};

const promiseQuery = (department_id, item_type, items, tx) => {
return new RSVP.Promise((resolve, reject) => {
tx.query('SELECT item FROM belongs_to_departments WHERE department=' + department_id + ' AND item_type=' + item_type)
.then((db_items)=> {
let queries = [];
let itemsToBeAdded = [];
let insert_query = 'INSERT INTO belongs_to_departments (item_type, department, item) VALUES ($(item_type), $(department), $(item))';

populateUpdateArray(items, db_items, itemsToBeAdded);
populateUpdateQuery(insert_query, itemsToBeAdded, department_id, item_type, queries, tx);

resolve(queries);
}).catch(() => {
reject();
});
});
};

const populateUpdateArray = (array_0, array_1, updateArray) => {
array_0.forEach((item) => {
if (array_1.indexOf(item) === -1) {
updateArray.push(item);
}
});
};

const populateUpdateQuery = (query, id_array, department_id, item_type, queries, tx) => {
id_array.forEach((item) => {
let values = {
item_type: item_type,
department: department_id,
item: item
};

queries.push(tx.none(query, values));
});
};

最佳答案

感谢 Vitaly 的帮助。这对我有用:

const update = data => {
const users = {
items: data.user,
item_type: 1,
id: data.department
}

db.tx(tx => {
const updateQueries = [];

updateQueries.push(promiseQuery(department.id, users.item_type, users.items, tx));

RSVP.all(updateQueries).then(results => {
// results is array of array, so i flatten it
const sqlUpdates = results.reduce((a, b) => { return a.concat(b); }, []);

return tx.batch(sqlUpdates);
});
}).then(() => {
res.sendStatus(204);
}).catch(err => {
console.log('error', err.message);
});
};

const promiseQuery = (department_id, item_type, items, tx) => {
return new RSVP.Promise((resolve, reject) => {
tx.query('SELECT item FROM belongs_to_departments WHERE department=' + department_id + ' AND item_type=' + item_type)
.then(db_items => {
const queries = [];
const itemsToBeAdded = [];
const insert_query = 'INSERT INTO belongs_to_departments (item_type, department, item) VALUES ($(item_type), $(department), $(item))';

populateUpdateArray(items, db_items, itemsToBeAdded);
populateUpdateQuery(insert_query, itemsToBeAdded, department_id, item_type, queries, tx);

resolve(queries);
}).catch(() => {
reject();
});
});
};

const populateUpdateArray = (array_0, array_1, updateArray) => {
array_0.forEach((item) => {
if (array_1.indexOf(item) === -1) {
updateArray.push(item);
}
});
};

const populateUpdateQuery = (query, id_array, department_id, item_type, queries, tx) => {
id_array.forEach(item => {
const values = {
item_type: item_type,
department: department_id,
item: item
};

queries.push(tx.none(query, values));
});
};

关于node.js - 解析 promise 数组 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40841128/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com