Knejx Js只承诺

时间:2018-11-11 11:31:59

标签: node.js express knex.js

如何使此功能起作用。只有诺言回到我身上。

codeProducts.forEach((code, index) => {

    const qt = app.db('products').where('code',code).first().then(result => result.quantity)
      data[index] = {
        code: code,
        quantity: qt
      }
    })


    return data

2 个答案:

答案 0 :(得分:0)

let data = codeProducts.map((code, index) => {

    return app.db('products').where('code',code).first()
    .then(result => {
        return {
        code: code,
        quantity: result.quantity
      }
    }) 
})  


return data

此代码应该可以解决您的问题。您正在访问承诺之外的数量。为了设置数据数组上的数量,您需要在then

内部进行设置

答案 1 :(得分:0)

有两种或三种方法可以解决此问题-

方法:1

我只会打一个数据库。(我爱我的数据库:D)as-

onDestroy()

方法:2(我不喜欢这种方法。对数据库的调用过多)

public class MainActivity extends AppCompatActivity implements Observer {

    private AlarmManager alarmManager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BroadcastObserver.getInstance().addObserver(this);
    }

    @Override
    public void update(Observable o, Object arg) {
        Intent intent = (Intent) arg;
        Log.e("MainActivity","Update method "+intent.getAction());
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                setAlarm();
            }
        }, 500);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!checkAlarm()) {
            setAlarm();
        }

    }

    @Override
    protected void onDestroy() {
        BroadcastObserver.getInstance().deleteObserver(this);
        super.onDestroy();
    }

    public void setAlarm() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 12);
        calendar.set(Calendar.MINUTE, 24);
        calendar.set(Calendar.SECOND, 0);
        if (calendar.before(Calendar.getInstance())) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, MyReceiver.class);
        intent.setAction(MyReceiver.ACTION_RECEIVER);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }

    public void setEndAlarm() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 12);
        calendar.set(Calendar.MINUTE, 6);
        calendar.set(Calendar.SECOND, 0);
        if (calendar.before(Calendar.getInstance())) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, EndReceiver.class);
        intent.setAction(EndReceiver.ACTION_RECEIVER);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }

    private boolean checkAlarm() {
        Intent intent = new Intent(this, MyReceiver.class);
        intent.setAction(MyReceiver.ACTION_RECEIVER);
        boolean isSet = PendingIntent.getBroadcast(this, 1001, intent, PendingIntent.FLAG_NO_CREATE) != null;
        Log.e("MainActivity", isSet + " :Alarm is set");
        return isSet;
    }

    private boolean checkEndAlarm() {
        Intent intent = new Intent(this, EndReceiver.class);
        intent.setAction(EndReceiver.ACTION_RECEIVER);
        boolean isSet = PendingIntent.getBroadcast(this, 1001, intent, PendingIntent.FLAG_NO_CREATE) != null;
        Log.e("MainActivity", isSet + " :Alarm is set");
        return isSet;
    }

    @Override
    protected void onStop() {
        super.onStop();
        cancelAlarm();
    }

    private void cancelAlarm() {
        Intent intent = new Intent(this, MyReceiver.class);
        intent.setAction(MyReceiver.ACTION_RECEIVER);
        final PendingIntent pendingIntent =
                PendingIntent.getBroadcast(MainActivity.this, 1001, intent,
                        PendingIntent.FLAG_NO_CREATE);
        if (pendingIntent != null) {
            alarmManager.cancel(pendingIntent);
            pendingIntent.cancel();
        }
    }
}