如何确保我的循环完成所有功能的运行?

时间:2018-11-02 09:26:54

标签: node.js rest authentication

我正在使用nodejs。

Manufacturer m = cmbManufacturer.SelectedValue as Manufacturer; //We will get currently selected manufacturer inside combobox m.Name = "Here we change name of it"; m.Update(); //Here we run function inside our class and it will update database and new name 是尝试调用外部rest api调用。

  1. 在C点,如何确保列表中的所有项目都已运行,包括在A点和B点?
  2. 如何确保B在A之后运行?

我尝试了/** * @description * A Person * * @class */ function Person(name) { /** * @description * Name of the person * @property {String} */ this.name = name; /** * @description * Another Name of the person * @property {String} */ this.anotherName = name; /** * @description * Another Name of the person * @property {String} * @name Person#AndEvenMoreNames */ } var oPerson = new Person("Peter"); oPerson. ,但是我还是偏偏

日志:

let request = API2.sendRequest(bookId);

代码:

if (i === updateList.length) {

DAO类示例:

2018-11-02T16:51:33.672 Success: 5 record(s). Fail: 0 record(s). Total 10 record(s).

安全级别:

'use strict';

//imports
const format = require('string-format');

let Converter = require('../common/converter');
let converter = new Converter();

const bookDao = require('../dao/bookDao.js');
const shelfDao = require('../dao/shelfDao.js');
const libDao = require('../dao/libDao.js');

const API2 = require('../common/API2.js');
const constant = require('../common/constant.js');

const security = require('../common/security.js');

function updateInfo() {

    let updateCountParams = [];
    let updateParams = [];

    let updateList = null;

    bookDao.getUpdateCount(updateCountParams, function (results) {

        if (results[0].RECCOUNT > 0) {

            bookDao.getUpdate(updateParams, function (results) {

                updateList = results;

                let removalList = [];
                let failureList = [];

                for (var i = 0; i < updateList.length; i++) {
                    let bookId = updateList[i].BOOKID;

                    let updateParam1 = [];
                    let updateParam2 = [];

                    let request = API2.sendRequest(bookId);

                    request
                        .buffer(true)
                        .end(function (err, res) {

                            if (err) {
                                failureList.push(bookId);
                                return console.error(err);
                            }

                            let data = {
                                body: res.body,
                                text: res.text
                            };

                            let data = data.text;

                            if (data == undebookIded || data == null) {
                                failureList.push(bookId);
                                return console.error("DATA NOT FOUND".red);
                            }

                            let privateKey = "./resource/priv_key.pem";

                            var jweParts = data.split(".");
                            security.decryptJWE(jweParts[0], jweParts[1], jweParts[2], jweParts[3], jweParts[4], privateKey)
                                .then(data => {
                                    if (data == undefined || data == null) {
                                        failureList.push(bookId);
                                        return console.error("INVALID DATA OR SIGNATURE FOR DATA".red);
                                    }

                                    let name = data.name.value;
                                    let desc = data.desc.value;
                                    let tag = data.tag.desc;
                                    let status = data.status.value;


                                    let updatedDate = converter.formatDate(new Date());
                                    let updatedBy = constant.DEFAULT__USERNAME;

                                    updateParam1.push(tag, updatedDate, updatedBy,
                                        bookId);

                                    updateParam2.push(tag, updatedDate, updatedBy,
                                        bookId);

                                    //Point A
                                    shelfDao.updateShelf(updateParam1, bookId, function (updateResult) {

                                        if (updateResult[1] === 1) {
                                            removalList.push(updateResult[0]);
                                        }
                                        else {
                                            failureList.push(updateResult[0]);
                                        }
                                    });

                                    //Point B
                                    libDao.updateLib(updateParam2, function (updateLibResult) {
                                        if (updateLibResult[1] === 1) {
                                            removalList.push(updateLibResult[0]);
                                        }
                                        else {
                                            failureList.push(updateLibResult[0]);
                                        }
                                    });

                                    //POINT C
                                    if (i === updateList.length) {

                                        //log number of success/fail/total.
                                        console.log(format(message.PROGRESS_MSG, removalList.length, failureList.length, updateList.length));
                                    }
                                })
                                .catch(error => {
                                    failureList.push(bookId);
                                    console.error("Error with decrypting JWE: %s".red, error);
                                })
                        });
                }
            });
        }
        else {
            return;
        }
    });
}

API2:

function updateShelf(params, bookId, callback) {
    pool.open(connString, function (err, conn) {

        conn.prepare(query.SQL_UPDATE_SHELF, function (error, stmt) {

            if (err) {
                console.error(err);
                return conn.closeSync();
            }

            stmt.executeNonQuery(params, function (err, result) {
                if( err ) {   
                    console.error(err);  
                }
                else {
                    console.debug("Affected rows = " + result);
                }

                //Close the connection
                conn.close();

                //return result as callback. 
                return callback([bookId, result]);
            });
        });
    });  
}

0 个答案:

没有答案