So I know the method app.set() is that setting value for a name. therefore, app.get() could get the value by name. but accordingly, if I set 'view engine' to 'ejs',where is the app.get('view engine')? if not have, how dose the app.set('view engine','ejs') work?
thank you for responding this wizard question.
It works as You expect:
How it works?
Check this: https://github.com/expressjs/express/blob/master/lib/application.js#L336
From line: 336
/**
* Assign `setting` to `val`, or return `setting`'s value.
*
* app.set('foo', 'bar');
* app.get('foo');
* // => "bar"
*
* Mounted servers inherit their parent server's settings.
*
* #param {String} setting
* #param {*} [val]
* #return {Server} for chaining
* #public
*/
app.set = function set(setting, val) {
if (arguments.length === 1) {
// app.get(setting)
return this.settings[setting];
}
debug('set "%s" to %o', setting, val);
// set value
this.settings[setting] = val;
// trigger matched settings
switch (setting) {
case 'etag':
this.set('etag fn', compileETag(val));
break;
case 'query parser':
this.set('query parser fn', compileQueryParser(val));
break;
case 'trust proxy':
this.set('trust proxy fn', compileTrust(val));
// trust proxy inherit back-compat
Object.defineProperty(this.settings, trustProxyDefaultSymbol, {
configurable: true,
value: false
});
break;
}
return this;
};
Related
Im trying to display a confirmation message when a record is correctly saved and an error message when its not.
But when I try to save the script I get
SuiteScript 2.x entry point scripts must implement one script type function
appears and i don't know why. Here is the script:
/**
* #NApiVersion 2.x
* #NScriptType ClientScript
*
*/
define('[N/ui/message]', function(message){
function saveRecord(context){
var currentRecord = context.currentRecord;
var pName = currentRecord.getValue('name');
var pAge = currentRecord.getValue('custrecord_p_age');
var pLastName = currentRecord.getValue('custrecord_plastname');
if(pName != null && pAge != null && pLastName != null ){
var confirmationMsg = message.create({
title: 'Enhorabuena',
message: 'La persona ah sido ingresada con exito',
Type: message.Type.CONFIRMATION
});
confirmationMsg.show({
duration: 5000
});
return true;
}else{
var errorMsg = message.create({
title: 'Enhorabuena',
message: 'La persona ah sido ingresada con exito',
Type: message.Type.ERROR
});
errorMsg.show({
duration: 5000
});
return false;
}
}
return{
saveRecord: saveRecord
}
});
Change
/** *#NApiVersion 2.x *#NScriptType ClientScript * */
to (remove the extra * towards the end of the tag)
/**
*#NApiVersion 2.x
*#NScriptType ClientScript
*/
Also change
define('[N/ui/message]', function(message){
to (move the single quotes to inside the brackets)
define(['N/ui/message'], function(message){
Try a stripped down version in the debugger with "Require." Your code seems fine. Also write it in Visual Code so you can see the formatting. Take out the functionality and see what is missing in structure for suitescript 2.0, example:
/**
* #NApiVersion 2.x
* #NScriptType Restlet
*/
define(["N/log"], function(log) {
function dostuff(context) {
//do stuff
}
return {
post: dostuff
};
});
I set up a simple Backendless API Service and am running it through CodeRunner. As a test, I'm simply getting a record from the database and returning it. I've tried every combination of return type definition in the class annotations that I can think of, and I've assured that the correct record exists and is being returned to the service, but I've never successfully had the record returned using the console, or via a SDK invocation. In every case, the body returned to the invocation is null. My current test uses "Object" as the return type for the getSchedule call - are database objects not objects?
Here is the entire service:
'use strict';
const { DateTime } = require("luxon");
const util = require("util");
class Scheduling {
/**
*
* #param {String} day
* #returns {Object}
*/
getSchedule( day ) {
let t = DateTime.fromISO(day).toMillis();
let q = Backendless.DataQueryBuilder.create().setWhereClause(`day = ${t}`);
Backendless.Data.of("schedules").find(q)
.then(rec => {
console.log(util.inspect(rec,3))
if (rec.length === 1) {
return rec[0]
}
else {
return {error: 404, msg: 'not found'}
}
})
}
}
Backendless.ServerCode.addService( Scheduling )
The "inspect" call indicates I am retrieving the correct record. No errors, the return status of the invocation is always 200. Obviously, I'm missing something about API service return types, please point me in the correct direction.
The problem is the response for the find method is returned after the invocation of getSchedule is complete (because the API invocation is asynchronous).
How about declaring the getSchedule with async and then await for the API invocation?
'use strict';
const { DateTime } = require("luxon");
const util = require("util");
class Scheduling {
/**
*
* #param {String} day
* #returns {Object}
*/
async getSchedule( day ) {
let t = DateTime.fromISO(day).toMillis();
let q = Backendless.DataQueryBuilder.create().setWhereClause(`day = ${t}`);
var rec = await Backendless.Data.of("schedules").find(q);
console.log(util.inspect(rec,3))
if (rec.length === 1) {
return rec[0]
}
else {
return {error: 404, msg: 'not found'}
}
}
}
Backendless.ServerCode.addService( Scheduling )
I'm using Expressjs
At /routes/index.js i have:
app.group('/test', test => {
const testHandler = new testHandler();
test.get('/test-action', testHandler.testAction.bind(testHandler));
});
At /test/handler.js i have
export class testHandler {
constructor() {
}
/**
* #param req
* #param res
* #param next
* #returns {*}
*/
async testAction(req, res, next) {
// todo code here
}
}
I want to create a cronjob to run that route ( for example localhost:3000/test/test-action ) twice an hour.
With PHP i can do it by * */2 * * * php /path_to_webroot/index.php param1 param2
Is there a similar way to do it with Nodejs?
You can use node-cron. It uses similar syntax as you are using in php.
# min hour mday month wday command
*/15 * * * * some-command
to schedule some-command to run every 15 minutes, node-cron would use a similar syntax to specify the time to run:
'0 */15 * * * *'
Well, you need to define your express routes normally. Then inside your cron function you would make a request to that express route you have defined.
request('http://localhost:3000/test/test-action', function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log('im ok')
// console.log(body)
}
})
You can use `request inside your corn job. This way your API will get called every specific time.
I am running into the issue below when trying to edit a CUSTOMER record in NetSuite. The script I have created is very simple.
What could I possibly doing wrong with such a simplistic piece of code?
{"type":"error.SuiteScriptModuleLoaderError","name":"MODULE_DOES_NOT_EXIST","message":"Module does not exist: /SuiteScripts/BillingInfoUpdated.js","stack":[]}
SCRIPT:
define(['N/log'], function (log) {
/**
* User Event 2.0 example showing usage of the Submit events
*
* #NApiVersion 2.x
* #NModuleScope SameAccount
* #NScriptType UserEventScript
* #appliedtorecord customer
*/
var exports = {};
function afterSubmit(scriptContext) {
log.debug({
"title": "After Submit",
"details": "action=" + scriptContext.type
});
}
exports.afterSubmit = afterSubmit;
return exports;
});
Add .js to the end of the script file name
Nathan Sutherland answer worked for me and it's absolutely fine But I'm writing this answer so new user get that faster and not confused with other names.
You need to add .js where the blue arrow points.
On starting while creating script.
if you forget then edit here
Use this instead:
var LOGMODULE; //Log module is preloaded, so this is optional
/**
*#NApiVersion 2.x
*#NModuleScope Public
*#NScriptType UserEventScript
*/
define(['N/log'], runUserEvent);
function runUserEvent(log) {
LOGMODULE = log;
var returnObj = {};
returnObj.afterSubmit = afterSubmit;
return returnObj;
}
function afterSubmit(context) {
log.debug('After Submit', "action=" + context.type);
//LOGMODULE.debug('After Submit', "action=" + context.type); //Alternatively
//context.newRecord; //Just showing how to access the records
//context.oldRecord;
//context.type;
return;
}
For more 2.0 Quickstart Samples: ursuscode.com
I am working on a fairly large project coming up, and I am (slowly) finding the ins and outs with requireJS. I can say I love it (the concept) and doubt I'll ever go back to "the old way." That said, I'd like to develop good habitats from the start.
I have a main module called application.js
'use strict';
/**
* Primary application module.
*
* #param {function} jquery | jQuery library. http://jquery.com/
* #returns {object}
*
*/
define([
'jquery'
], function (jquery) {
var Application = {
/**
* Initialization method.
*
* #returns {void}
*
*/
initalize: function () {
// Start doing stuff...
}
};
return Application;
});
So far so good.
What I' curious about, is when I start adding more and more modules. Let's say at some point I have a "contact us" form or some other module that doesn't necessarily need loaded at start up, but will need to be available on page "x".
Would I load it in the list of initial dependencies like this:
'use strict';
/**
* Primary application module.
*
* #param {function} jquery | jQuery library. http://jquery.com/
* #param {object} moduleone | ModuleOne
* #returns {object}
*
*/
define([
'jquery'
'moduleone'
], function (jquery, ModuleOne) {
var Application = {
/**
* Initialization method.
*
* #returns {void}
*
*/
initalize: function () {
// Start doing stuff...
if ($('#moduleOneThing').length) {
// Do stuff with ModuleOne...
}
}
};
return Application;
});
Or is it better to set another define/require block like this:
'use strict';
/**
* Primary application module.
*
* #param {function} jquery | jQuery library. http://jquery.com/
* #param {object} moduleone | ModuleOne
* #returns {object}
*
*/
define([
'jquery'
], function (jquery) {
var Application = {
/**
* Initialization method.
*
* #returns {void}
*
*/
initalize: function () {
// Start doing stuff...
if ($('#moduleOneThing').length) {
require(['moduleone'], function (ModuleOne) {
// Do stuff with Module One...
});
}
}
};
return Application;
});
Or, perhaps I'm way off on everything. In any case I would like to learn the "correct" way from the start in hopes to not have to break bad habits down the road. This is an extremely simple example, but the complexity will grow quickly and I am hopeful to set things up correctly to avoid a pile of steaming garbage in the next few weeks.
Thank you for your time!