在JavaScript/Node.js中运行.map时,如何跳过回调? [英] How do I skip a callback when running .map in JavaScript/Node.js?

查看:59
本文介绍了在JavaScript/Node.js中运行.map时,如何跳过回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的续集这个问题,我需要在POST请求中接受多个对象,然后为每个对象处理它,将其保存,然后将保存的对象返回到前端(以便客户端可以看到成功编辑了哪些列)

Kind of a sequel to this question, I need to accept multiple objects in a POST request and then for each object process it, save it, and then return the saved object to the frontend (so that the client can see which columns were successfully edited).

当我使用.map时,它确实保存到数据库中,我可以确认这一点.但是,我有两个问题:

When I use .map, it does save to the database and I can confirm this. However, I have two problems:

  1. 它无法正确执行res.locals.retval.addData(dtoObject);,并且我返回的有效负载内部没有数据传输对象.
  2. 我的对象验证无法在map的回调内部完成.我最初尝试使用reduce,但是根本不起作用,只是将所有相同的值保存到每个数据库对象中.在映射无效的JSON对象时,如何排除它们?

  1. It does not execute res.locals.retval.addData(dtoObject); correctly, and my returning payload has no data transfer objects inside of it.
  2. My object validation cannot be done inside of the callback of map. I initially tried reduce, but that didn't work at all and just saved all the same values to each database object. How can I exclude invalid JSON objects while I'm mapping them?

var jsonObjects = req.body;
//for (var n in req.body) {
var promises = jsonObjects.map((jsonObject) => {
    var transform = new Transform();

    // VALIDATION OF jsonObject VARIABLE IS HERE

    if (jsonObject.id == 0) {
        var databaseObject = Database.getInstance().getModel(objectName).build(jsonObject);

        transform.setNew(true);
        transform.setJsonObject(jsonObject);
        transform.setDatabaseObject(databaseObject);

        transform.baseExtract()
        .then(() => transform.extract())
        .then(() => transform.clean())
        .then(() => transform.getDatabaseObject().save())
        .then(function(data) {
            // PROCESSING DATA
        }).catch((e) => {
            // ERROR
        });
    } else {
        var queryParameters = {
            where: {id: jsonObject.id}
        };
        console.log("Query parameters: ");
        console.log(queryParameters);
        Database.getInstance().getModel(objectName).findOne(queryParameters).then((databaseObject) => {
            transform.setJsonObject(jsonObject);
            transform.setDatabaseObject(databaseObject);
        })
        .then(() => transform.baseExtract())
        .then(() => transform.extract())
        .then(() => transform.clean())
        .then(() => transform.getDatabaseObject().save())
        .then((data) => {
            // PROCESSING DATA
        }).catch((e) => {
            // ERROR
        });
    }
});

Promise.all(promises)
.then((results) => {
    return next();
}).catch((e) => {
    throw e;
});

以下是产生的有效载荷:

Here's the resulting payload:

{
  "errors": [],
  "warnings": [],
  "data": []
}

推荐答案

  1. 正如@KevinB在评论中所说,您错过了箭头函数内的return调用,因此数据库保存正在进行,因为它们是Promise链的一部分,但是对响应的推送被拖延了,等待返回,然后Express.js调用会在Promises之前解析.将return Database.getInstance()return transform.baseExtract()添加到您的代码中以解决此问题.
  2. 使用 Array.prototype .filter()删除要忽略的元素,因为您将不需要在它们上执行Promises,然后调用

  1. As @KevinB said in the comments, you are missing the return calls inside of your arrow functions so the database saves are going through because they are part of the Promise chain, but pushes to the response are stalled waiting for the return, and then the Express.js call resolves before the Promises do. Add return Database.getInstance() and return transform.baseExtract() to your code to fix this.
  2. Use Array.prototype.filter() to remove elements you want to ignore since you won't ever need to execute Promises on them, then call Array.prototype.map() on the resulting array. If you don't want to use the arrow functions, you can specify this as a parameter to filter and map:

jsonObjects.filter(function(jsonObject){

jsonObjects.filter(function(jsonObject) {

},此);

var promises = jsonObjects.map(function(jsonObject){

var promises = jsonObjects.map(function(jsonObject) {

},此);

这篇关于在JavaScript/Node.js中运行.map时,如何跳过回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆