Ich habe versucht, Versprechen in eine Funktion hinzuzufügen, ich habe diesen Fehler TypeError: sort_devices_name.then ist keine Funktion, Funktion get_groups_devices ruft sort_devices_name und sort_devices_name ist die Funktion, die ich als das Versprechen verwenden möchte .Versprechen in einer Funktion
});
// 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 {
Sie rufen Ihre Funktion derzeit nicht auf, sondern greifen nur auf das Funktionsobjekt zu, das selbst keine 'then()' Methode hat. Rufen Sie die Funktion 'sort_devices_name(). then()' auf. – Sirko
@Sirko Danke, es funktioniert jetzt gut. –