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
appdirectly in the model script—instead, useCustomer.appto get the LoopBack app reference tied to the Customer model. This avoids "app is not defined" errors. - beforeRemote parameters: The second parameter in the
beforeRemotehook is the upcoming Customer instance (which doesn't exist yet), so we use a placeholder like_to ignore it. The critical parameters arectx(request context holding your input data) andnext(to control the workflow). - Correct create method syntax: LoopBack's
model.create()follows this structure:- The data to save to the target model
- Optional options (like passing the current user for permission checks)
- 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 tonext(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
customerDetailIdproperty to your Customer model, then set it to the ID of the newly createdcustomerdetailbefore callingnext(). This lets you link the two records later if needed.
Next steps to verify:
- Double-check that your
customerdetailmodel has all the fields present incust_cmp_profile—mismatched fields will trigger validation errors. - Test the endpoint: When you create a new Customer with
cust_cmp_profiledata, check if a correspondingcustomerdetailentry appears in your database. - If you hit permission issues, make sure the
customerdetailmodel'screatemethod allows access from your current request context (adjust ACLs if necessary).
内容的提问来源于stack exchange,提问作者Rocky
相关产品推荐
相关产品推荐

