I have ExtentedUser Model which has two hasOne relationships to two models
driver and customer (both having a belongTo realtionship back to the ExtentedUser) and they are also the Extended from User base model).
after the use of
ExtentedUser.afterRemote('create', function(context, user, next){
//in here i am unable to get
// reference to driver or customer model
//eg user.driver or user.customer
}
My bad what i was doing wrong is that i using model name as property.
user.driver /where driver is modelName
but i should have used the relationship name
which was
user.drivers /where drivers is RelationShipName
Related
i am using Strapi for a prototype and i am meeting the following issue. I have created a new content type "Checklist" and i added in it a relation property 1 to many with the User model provided by the users-permissions plugin.
Then i wanted to add some custom logic on the lifecycle call back, in beforeSave and in beforeUpdate from which i would like to access the user assigned to the Checklist.
The code looks like that:
{
var self = module.exports = {
// Before saving a value.
// Fired before an `insert` or `update` query.
generateLabel : (model) => {
var label = "";
var day = _moment(model.date,_moment.ISO_8601).year();
var month = _moment(model.date,_moment.ISO_8601).day();
var year = _moment(model.date,_moment.ISO_8601).month();
console.log(model);
if (model.user) {
label = `${model.user}-${year}-${month}-${day}`;
}else{
label = `unassigned-${year}-${month}-${day}`;
}
return label;
I call the method generateLabel from the callback. It works, but my model.user always returned undefined. It is a 1-n property. I can access model.date property (one of the field i have created) without any issue, so i guess the pbs is related to something i have to do to populate the user relation, but i am not sure on how to proceed.
When i log the model object, the console display what i guess is a complete mongoose object but i am not sure where to go from there as if i try to access the property that i see in the console, i will always reach an undefined.
Thanks in advance for your time, i use the following
strapi: 3.0.0-alpha.13.0.1
nodejs: v9.10.1
mongodb: 3.6.3
macos high sierra
Also running into the similar / same issue, I think this has to do with the user permissions plugin, and having to use that to access the User model. Or I thought about trying to find the User that’s associated with the id of the newly created record. I’m trying to use AfterCreate. Anyone that could shed some light on this would be great!
It's because relational attributes are not send in create fonction (See your checklist service add function).
Relations are handled in an other function updateRelations.
The thing you can do is to send values in Checklit.create()
I'm using Loopback v3, and when I try to reference another model inside
another one, eg. customer.js, with
module.exports = function(Customer) {
var Product = Customer.app.models.Product;
it crashes saying
TypeError: Cannot read property 'models' of undefined
Any thoughts?
Have you added a relation between both models?
Make sure you have defined a relation between Customer and Product models on Customer.json and Product.json.
This can be done manually or by using the loopback generator.
slc loobpack:relation
More information here: http://loopback.io/doc/en/lb2/Creating-model-relations.html
Assuming I have Model and RelatedModel types in my application, Model.hasMany(RelatedModel) and RelatedModel.belongsTo(Model, {foreignKey:{allowNull: false, name: 'model_id'}});, is it possible to build Model instance with RelatedModel set?
When I'm using
var model = Model.build({});
model.createRelatedModel({});
model.save();
application fails, as model does not have id when built and id assigned only after save() called, so relatedModel ending up with model_id set to NULL.
Is it possible to work with non-persistent model and it's relations and then save all with model.save()? (using transaction will be also nice)
I am using Sequlizejs 2.0.0-rc4.
I have two entities: Feeds (containing an owner_id field) and Owners. Basically, there is a one-to-many relationship between Feeds and Owners - an Owner has many Feeds.
I have tried modelling this many different ways, but the only way I got it right is:
Owner.hasMany(Feed, {foreignKey: 'owner_id'});
Feed.belongsTo(Owner, {foreignKey: 'owner_id'});
The only problem with this is the fact that when I try to get a Feed like so:
models.feeds.findAll({
include: [models.owners]
}).then(function (feeds) {
// feeds will contain a SequlizeInstance object
});
Then the result will contain a SequlizeInstance object instead of a Owner object, which I then have to access through something like feeds.owners.dataValues.
What is the problem here?
There is no problem here :). Sequelize returns sequelize instances because they are what allows you to do all the lovely sequelize stuff, such as feed.owner.updateAttributes({.. }).
If you want the plain values from the owner object, you should call get or toJSON
I have a UserService object that is essentially a Service with additional configuration parameters and is attached to a User. In my View I would like to render a list of these UserServices however the model is formed as such:
UserService = Backbone.Model.extend({
defaults: {
id: 0,
user_id: 0, // This needs to reference the user object somehow
service_id: 0, // This needs to reference the service object somehow
length: 216000,
price: 1000
}
});
If I bind this model to the view, what is rendered ends up being the service_id instead of the parameter I need to render: service.name.
My questions are:
What should be stored in the UserService model at service? The full service object? Mongoose ID? Some other ID? (Please specify a suggestion)
Where should I get the information for this service.name / When should I pull the Service object to get that information? It would be nice to be able to do service.name in the view when rendering...
Is there a function to chain--upon loading the model, load related models that are needed?
Overall I just need an understanding of how related models work in Backbone / Express / Mongoose.
Any help is appreciated!
Update: After doing a bit of reading I have a couple different methods I can see:
Within the constructor / initializer load the Service object into the UserService object based on the reference ID returned from the server.
My questions with that one then become... what is the reference ID? Where do I put the newly retrieved object into, possibly in place of the ID?
Use the toJSON method to return an asthetic version of the UserService where it retreives the Service object and would return an object with the service name in it's place:
{
id: ???,
service_name: "this was retrieved from the service object in the toJSON method",
length: "1 hour", // converted from within the toJSON method
price: 10.00 // converted from cents to dollars in the toJSON method
}
Or maybe a combination? Thoughts?
Parse models handle loading related entities well, there is also library called Backbone Relational that can help with this.
Otherwise, my best recommendation is to store the object's ID and fetch the related entity upon success of fetching the first entity.
Anyone needing a code example just comment here and I'll see what I can come up with.