gpt4 book ai didi

javascript - 函数中的 Promise

转载 作者:行者123 更新时间:2023-12-01 04:08:04 25 4
gpt4 key购买 nike

我一直在尝试将 Promise 添加到函数中,但出现此错误 TypeError: sort_devices_name.then 不是函数,函数 get_groups_devices 正在调用 sort_devices_name ,而 sort_devices_name 是我想用作 Promise 的函数。

 });
// Find devices object id with relation of groups
get_groups_devices();
}
});

function sort_devices_name() {
return new Promise(
function (resolve, reject) {
var d_ids = data.map(function (d) {
return ObjectId(d.d_id);
});
devices_lookup.find({"_id": {$in: d_ids}}).sort({"device_name": 1}).toArray(function (err, d_data) {
if (err) {
return res.send(JSON.stringify(err));
}
else {
var modify_data = [];
d_data.forEach(function (val) {
data.forEach(function (val2) {
if (val2.d_id == val._id) {
modify_data.push({g_id: val2.g_id, d_id: val._id});
}
});
});
resolve(modify_data);
}
});
});
}



// Find devices object id with relation of groups (if any) from devices group relation table.
function get_groups_devices() {
devices_group.find({"g_id": {$in: groups_ids}}, {
"d_id": 1,
"g_id": 1,
"_id": 0
}).toArray(function (err, d_data) {
d_data = sort_devices_name(d_data);
sort_devices_name.then(function (d_data) {
console.log(d_data);
if (err) {
return res.send(JSON.stringify(err));
} else {

enter image description here

最佳答案

您需要调用sort_devices_name,而不仅仅是引用它;添加():

sort_devices_name().then(function...
// --------------^^

函数没有then属性,但它返回的promise有。

关于javascript - 函数中的 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41659644/

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