keystone.js insert into relationship fields












0














I'm really sorry if this comes out naive, but I've been at it for far too long.
My issue is related to this unanswered question. I have Keystone 4 installed, and need to use its update feature. Here's a country model:



'use strict';

const keystone = require('keystone');
const Types = keystone.Field.Types;
/**
* Country Model
* ============
*/

let Country = new keystone.List('Country', {
autokey: { from: 'name', path: 'key', unique: true },
});
Country.add({
name: { type: Types.Text, initial: true, required: true, unique: true },
capital: { type: Types.Relationship, ref: 'State' },
});

// Get country's id
Country.schema.methods.getCountryId = function (name, cb) {
this.model.findOne({ name: name }).select('_id').exec(cb);
};

Country.register();


the getCountryId() method is so I could supply id's in the relationship field when creating state updates, with model shown below:



'use strict'
;
const keystone = require('keystone');
const Types = keystone.Field.Types;
/**
* State Model
* ============
*/

let State = new keystone.List('State', {
autokey: { from: 'name', path: 'key', unique: true },
});
State.add({
name: { type: Types.Text, initial: true, required: true, unique: true },
capital: { type: Types.Text },
country: { type: Types.Relationship, initial: false, ref: 'Country', many: false, index: true }
});

// Return only a State's _id
State.statics = {
getStateId: function (name, cb) {
this.findOne({ name: name }).select('_id').exec(cb);
}
};

State.register();


Now the state updates, for brevity:



'use strict';
const keystone = require('keystone');

let Country = keystone.list('Country');

// Get the _id of France
let france = Country.getCountryId('France', function (err, country) {
if (err) {
// handle error, express example:
console.err(err);
}

exports.create = {
State: [
{ name: 'Nouvelle-Aquitaine', capital: 'Bordeaux', country: france },
{ name: 'Auvergne-Rhône-Alpes', capital: 'Lyon', country: france },
],
};


My current commit is massively leaking memory. But prior to that, I kept getting Country.getCountryId is not a function. I've also tried the concept of Mongoose Populate, which seems to only work when using one of the find* functions.
Obviously I'm missing something. Please tell me what it is.










share|improve this question






















  • I'll probably just discard the idea of application updates entirely
    – user6778534
    Nov 21 '18 at 12:03
















0














I'm really sorry if this comes out naive, but I've been at it for far too long.
My issue is related to this unanswered question. I have Keystone 4 installed, and need to use its update feature. Here's a country model:



'use strict';

const keystone = require('keystone');
const Types = keystone.Field.Types;
/**
* Country Model
* ============
*/

let Country = new keystone.List('Country', {
autokey: { from: 'name', path: 'key', unique: true },
});
Country.add({
name: { type: Types.Text, initial: true, required: true, unique: true },
capital: { type: Types.Relationship, ref: 'State' },
});

// Get country's id
Country.schema.methods.getCountryId = function (name, cb) {
this.model.findOne({ name: name }).select('_id').exec(cb);
};

Country.register();


the getCountryId() method is so I could supply id's in the relationship field when creating state updates, with model shown below:



'use strict'
;
const keystone = require('keystone');
const Types = keystone.Field.Types;
/**
* State Model
* ============
*/

let State = new keystone.List('State', {
autokey: { from: 'name', path: 'key', unique: true },
});
State.add({
name: { type: Types.Text, initial: true, required: true, unique: true },
capital: { type: Types.Text },
country: { type: Types.Relationship, initial: false, ref: 'Country', many: false, index: true }
});

// Return only a State's _id
State.statics = {
getStateId: function (name, cb) {
this.findOne({ name: name }).select('_id').exec(cb);
}
};

State.register();


Now the state updates, for brevity:



'use strict';
const keystone = require('keystone');

let Country = keystone.list('Country');

// Get the _id of France
let france = Country.getCountryId('France', function (err, country) {
if (err) {
// handle error, express example:
console.err(err);
}

exports.create = {
State: [
{ name: 'Nouvelle-Aquitaine', capital: 'Bordeaux', country: france },
{ name: 'Auvergne-Rhône-Alpes', capital: 'Lyon', country: france },
],
};


My current commit is massively leaking memory. But prior to that, I kept getting Country.getCountryId is not a function. I've also tried the concept of Mongoose Populate, which seems to only work when using one of the find* functions.
Obviously I'm missing something. Please tell me what it is.










share|improve this question






















  • I'll probably just discard the idea of application updates entirely
    – user6778534
    Nov 21 '18 at 12:03














0












0








0







I'm really sorry if this comes out naive, but I've been at it for far too long.
My issue is related to this unanswered question. I have Keystone 4 installed, and need to use its update feature. Here's a country model:



'use strict';

const keystone = require('keystone');
const Types = keystone.Field.Types;
/**
* Country Model
* ============
*/

let Country = new keystone.List('Country', {
autokey: { from: 'name', path: 'key', unique: true },
});
Country.add({
name: { type: Types.Text, initial: true, required: true, unique: true },
capital: { type: Types.Relationship, ref: 'State' },
});

// Get country's id
Country.schema.methods.getCountryId = function (name, cb) {
this.model.findOne({ name: name }).select('_id').exec(cb);
};

Country.register();


the getCountryId() method is so I could supply id's in the relationship field when creating state updates, with model shown below:



'use strict'
;
const keystone = require('keystone');
const Types = keystone.Field.Types;
/**
* State Model
* ============
*/

let State = new keystone.List('State', {
autokey: { from: 'name', path: 'key', unique: true },
});
State.add({
name: { type: Types.Text, initial: true, required: true, unique: true },
capital: { type: Types.Text },
country: { type: Types.Relationship, initial: false, ref: 'Country', many: false, index: true }
});

// Return only a State's _id
State.statics = {
getStateId: function (name, cb) {
this.findOne({ name: name }).select('_id').exec(cb);
}
};

State.register();


Now the state updates, for brevity:



'use strict';
const keystone = require('keystone');

let Country = keystone.list('Country');

// Get the _id of France
let france = Country.getCountryId('France', function (err, country) {
if (err) {
// handle error, express example:
console.err(err);
}

exports.create = {
State: [
{ name: 'Nouvelle-Aquitaine', capital: 'Bordeaux', country: france },
{ name: 'Auvergne-Rhône-Alpes', capital: 'Lyon', country: france },
],
};


My current commit is massively leaking memory. But prior to that, I kept getting Country.getCountryId is not a function. I've also tried the concept of Mongoose Populate, which seems to only work when using one of the find* functions.
Obviously I'm missing something. Please tell me what it is.










share|improve this question













I'm really sorry if this comes out naive, but I've been at it for far too long.
My issue is related to this unanswered question. I have Keystone 4 installed, and need to use its update feature. Here's a country model:



'use strict';

const keystone = require('keystone');
const Types = keystone.Field.Types;
/**
* Country Model
* ============
*/

let Country = new keystone.List('Country', {
autokey: { from: 'name', path: 'key', unique: true },
});
Country.add({
name: { type: Types.Text, initial: true, required: true, unique: true },
capital: { type: Types.Relationship, ref: 'State' },
});

// Get country's id
Country.schema.methods.getCountryId = function (name, cb) {
this.model.findOne({ name: name }).select('_id').exec(cb);
};

Country.register();


the getCountryId() method is so I could supply id's in the relationship field when creating state updates, with model shown below:



'use strict'
;
const keystone = require('keystone');
const Types = keystone.Field.Types;
/**
* State Model
* ============
*/

let State = new keystone.List('State', {
autokey: { from: 'name', path: 'key', unique: true },
});
State.add({
name: { type: Types.Text, initial: true, required: true, unique: true },
capital: { type: Types.Text },
country: { type: Types.Relationship, initial: false, ref: 'Country', many: false, index: true }
});

// Return only a State's _id
State.statics = {
getStateId: function (name, cb) {
this.findOne({ name: name }).select('_id').exec(cb);
}
};

State.register();


Now the state updates, for brevity:



'use strict';
const keystone = require('keystone');

let Country = keystone.list('Country');

// Get the _id of France
let france = Country.getCountryId('France', function (err, country) {
if (err) {
// handle error, express example:
console.err(err);
}

exports.create = {
State: [
{ name: 'Nouvelle-Aquitaine', capital: 'Bordeaux', country: france },
{ name: 'Auvergne-Rhône-Alpes', capital: 'Lyon', country: france },
],
};


My current commit is massively leaking memory. But prior to that, I kept getting Country.getCountryId is not a function. I've also tried the concept of Mongoose Populate, which seems to only work when using one of the find* functions.
Obviously I'm missing something. Please tell me what it is.







javascript mongoose keystonejs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 11:42







user6778534



















  • I'll probably just discard the idea of application updates entirely
    – user6778534
    Nov 21 '18 at 12:03


















  • I'll probably just discard the idea of application updates entirely
    – user6778534
    Nov 21 '18 at 12:03
















I'll probably just discard the idea of application updates entirely
– user6778534
Nov 21 '18 at 12:03




I'll probably just discard the idea of application updates entirely
– user6778534
Nov 21 '18 at 12:03

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411302%2fkeystone-js-insert-into-relationship-fields%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411302%2fkeystone-js-insert-into-relationship-fields%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python