You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Loopback无关模型间如何传递数据?新手技术求助

How to Pass Data Between Unrelated Models in LoopBack

Hey there! Let's work through your problem since you're new to LoopBack. Your goal to create a customerdetail entry when a new Customer is created makes total sense, but there are a few tweaks needed in your code to get it working properly.

First, let's fix the issues in your current code

Here's a revised version with clear explanations:

module.exports = function(Customer) {
  Customer.beforeRemote('create', function(ctx, _, next) {
    // 1. Correctly fetch the LoopBack app instance tied to the Customer model
    const app = Customer.app;
    const CustomerDetail = app.models.customerdetail;

    // 2. Extract the specific data you want to pass to the customerdetail model
    const customerDetailData = ctx.args.data.cust_cmp_profile;
    
    // 3. Properly call the create method for customerdetail
    CustomerDetail.create(customerDetailData, { currentUser: ctx.req.user }, function(err, createdDetail) {
      if (err) {
        // If creating the detail fails, pass the error to next() to halt the Customer creation process
        return next(err);
      }

      // Optional: Link the created customerdetail to the Customer (if you want to establish an association later)
      // For example, add the detail's ID to the Customer data being created
      ctx.args.data.customerDetailId = createdDetail.id;

      // 4. Proceed with the original Customer create operation
      next();
    });
  });
};

Key fixes and explanations:

  • Accessing the app instance: You can't use app directly in the model script—instead, use Customer.app to get the LoopBack app reference tied to the Customer model. This avoids "app is not defined" errors.
  • beforeRemote parameters: The second parameter in the beforeRemote hook is the upcoming Customer instance (which doesn't exist yet), so we use a placeholder like _ to ignore it. The critical parameters are ctx (request context holding your input data) and next (to control the workflow).
  • Correct create method syntax: LoopBack's model.create() follows this structure:
    1. The data to save to the target model
    2. Optional options (like passing the current user for permission checks)
    3. A callback to handle success or errors
  • Error handling: Always check for errors in the callback. If something goes wrong creating the customerdetail, pass the error to next(err)—this tells LoopBack to stop the Customer creation process and return the error to the client.
  • Optional association: Even if the models are "unrelated" right now, you can add a customerDetailId property to your Customer model, then set it to the ID of the newly created customerdetail before calling next(). This lets you link the two records later if needed.

Next steps to verify:

  1. Double-check that your customerdetail model has all the fields present in cust_cmp_profile—mismatched fields will trigger validation errors.
  2. Test the endpoint: When you create a new Customer with cust_cmp_profile data, check if a corresponding customerdetail entry appears in your database.
  3. If you hit permission issues, make sure the customerdetail model's create method allows access from your current request context (adjust ACLs if necessary).

内容的提问来源于stack exchange,提问作者Rocky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:02:28