在环回中对持久化模型进行向上插入后,如何获取自动ID? [英] How to get auto Id after upsert on a persisted model in loopback?

查看:106
本文介绍了在环回中对持久化模型进行向上插入后,如何获取自动ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用looback-connector postgresql从postgresql db生成的模型.这些模型的id列是postgresql db的自动递增的整数列.

I have a some models generated from postgresql db using looback-connector postgresql. Id column of these models is a auto incremented integer column of postgresql db.

1)我在一个持久模型中添加了一个远程方法,在该模型中,我执行简单的更新或insert(upsert.

1) I have a remote method added on one of persisted models, where i perform simple update or insert(upsert.

 Car.CreateOrUpdateCar = function (carobj, req)     {
    Car.upsert(Carobj, function (err, Car) {
        if (err)
            console.log(err);
        else {
            req(err, Car);
        }
    });
};

2)已经添加了一个远程钩子,以在此远程方法之后执行.

2) have added a remote hook to execute after this remote method.

       Car.afterRemote('CreateOrUpdateCar', function (context, remoteMethodOutput, next) {
//Remaining code goes here
      next();
        });

3)我想在步骤(2)中提到的远程挂钩中,在步骤(1)中使用新插入的行的 Id

3) I want to use Id of newly inserted row in step (1), in the remote hook mentioned in step (2)

推荐答案

您可以像这样在远程挂钩中访问生成的ID:

You can access to generated id in remote hook like this :

 Car.afterRemote('CreateOrUpdateCar', function (context, remoteMethodOutput, next) {
  var genId = remoteMethodOutput.id || context.result.id;
      next();
        });

这篇关于在环回中对持久化模型进行向上插入后,如何获取自动ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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