Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Promise内部Promise在bluebird节点中创建不可管理的结构_Javascript_Node.js - Fatal编程技术网

Javascript Promise内部Promise在bluebird节点中创建不可管理的结构

Javascript Promise内部Promise在bluebird节点中创建不可管理的结构,javascript,node.js,Javascript,Node.js,我的查询如下: checkUserId(user_email).then((info) => { changeBookStatus(bookInfo._id, borrow).then(() => { issueReturnBook(info._id, bookInfo._id, due_date + "/" + due_month, bor

我的查询如下:

checkUserId(user_email).then((info) => {
                    changeBookStatus(bookInfo._id, borrow).then(() => {
                        issueReturnBook(info._id, bookInfo._id,
                            due_date + "/" + due_month, borrow).then((savedInfo) => {
                            console.log("saved info " + savedInfo._id);
                            LibraryTransaction.findById(savedInfo._id).
                            populate('UserDetails').populate('BookDetails').exec((err, libInfo) => {
                                res.json({
                                    status: '200',
                                    message: 'transaction completed',
                                    data: libInfo
                                });
                            });

                        }).catch((err) => {
                            res.json({
                                status: '500',
                                message: err
                            });
                        });
                    }).catch((err) => {
                        res.json({
                            status: '500',
                            message: err
                        });
                    });
                })


const issueReturnBook = (user_id, book_id, due_date, borrow) => {
    let borrowReturn;
    if (borrow == '0') {
        borrowReturn = false;
    } else if (borrow == '1') {
        borrowReturn = true;
    }
    return new Promise((resolve, reject) => {
        const libraryTransaction = new LibraryTransaction();
        libraryTransaction.UserDetails = user_id;
        libraryTransaction.BookDetails = book_id;
        libraryTransaction.DueDate = due_date;
        libraryTransaction.BorrowReturn = borrowReturn;
        libraryTransaction.save().then((info) => {
            if (!info) {
                reject(false);
            }
            resolve(info);
        }).catch((err) => {
            reject(err);
        });
    });
};


const changeBookStatus = (book_id, borrow) => {
    let borrowStat;
    if (borrow == '0') {
        borrowStat = false;
    } else if (borrow == '1') {
        borrowStat = true;
    }
    return new Promise((resolve, reject) => {
        Books.findOneAndUpdate({ '_id': book_id }, { $set: { CurrentlyAvailableStatus: borrow } }).then((info) => {
            if (!info) {
                reject('there is no such book');
            }
            resolve(info);
        }).catch((err) => {
            reject(err);
        });
    });
};

const checkUserId = (user_email) => {
    return new Promise((resolve, reject) => {
        User.findOne({ 'Email': user_email.trim() }).then((info) => {
            if (!info) {
                reject('the is no such user');
            }
            resolve(info);
        }).catch((err) => {
            reject(err);
        });
    });
};

现在,里面有太多了,那么,有没有更好的方法来有效地执行它呢?

很难判断哪些变量在范围内,哪些库事务函数返回承诺。但我想你想要这样的东西:

checkUserId(user_email)
    .then(info => {
        return changeBookStatus(bookInfo._id, borrow)
            // This is here so we can return "info" to the next function. You might want to
            // wrap this "changeBookStatus(...).then(...)" part in its own function.
            .then(() => {
                return info;
            });
    })
    .then(info => {
        return issueReturnBook(info._id, bookInfo._id, due_date + "/" + due_month, borrow);
    })
    .then(savedInfo => {
        console.log("saved info " + savedInfo._id);
        LibraryTransaction
            .findById(savedInfo._id)
            .populate('UserDetails')
            .populate('BookDetails').exec((err, libInfo) => {
                res.json({
                    status: '200',
                    message: 'transaction completed',
                    data: libInfo
                });
            });

    })
    .catch(err => {
        res.json({
            status: '500',
            message: err
        });
    });
请注意,我包含了返回语句,以明确每个承诺都需要返回,以便正确链接。但是你可以移除它们来清理东西:

checkUserId(user_email)
    // The extra ".then" is still here for the reasons mentioned above
    .then(info => changeBookStatus(bookInfo._id, borrow).then(() => info))
    .then(info => issueReturnBook(info._id, bookInfo._id, due_date + "/" + due_month, borrow))
    .then(savedInfo => {
        console.log("saved info " + savedInfo._id);
        LibraryTransaction
            .findById(savedInfo._id).
            populate('UserDetails')
            .populate('BookDetails').exec((err, libInfo) => {
                res.json({
                    status: '200',
                    message: 'transaction completed',
                    data: libInfo
                });
            });

    })
    .catch(err => {
        res.json({
            status: '500',
            message: err
        });
    });

书中几乎所有的都有。我应该这样写吗thensavedInfo=>return savedInfo而不是thensavedInfo=>{}这样做没有意义。thensavedInfo=>savedInfo-这与没有then是一样的。我想你还需要一个关于Library事务的承诺…填充…执行-目前它忽略了错误。最好也为此编写一个额外的Promission函数。