How can I retrieve a swagger path parameter from nodejs?
The swagger definition:
/objects/{id}:
x-swagger-router-controller: object.controller
get:
summary: Get object
operationId: getObject
consumes:
- application/json
parameters:
- in: path
name: id
type: string
required: true
What I've tried:
req.swagger.params['id'].value
req.query.id
Is there any dependency I need? My current dependencies are
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.12.3",
"morgan": "^1.9.1",
"swagger-express-mw": "^0.7.0",
"swagger-tools": "^0.10.4",
"swagger-ui": "^3.20.5",
"web3": "^1.0.0-beta.36"
}
Change this: req.swagger.params['id'].value --> req.swagger.params.id.value.
If you're using express framework and express router you can access request parameters by their name. For example this /objects/{id}: path parameter can be accessed like: req.params.id
Related
I am working in a GraphQL project using node + typegraphql + apollo-server.
My goal is to have specific metrics for regular queries, and separately, from federations. Is it possible to differentiate it in the server that is being requested and resolves the data?
By regular query I mean:
Client asks for User -> Apollo Gateway Server -> User Apollo Server resolves the User
And by a federated one I mean:
Client asks for Profile (which contains external User) -> Apollo Gateway Server -> User Apollo Server resolves the User
Apparently, this is possible using the DGS framework, #DgsQuery is for regular resolvers and #DgsEntityFetcher(name = "User") would work for the federation resolver.
The project related dependencies are:
"dependencies": {
"#apollo/federation": "^0.30.0",
"apollo-datasource-rest": "^3.1.1",
"apollo-graphql": "^0.9.3",
"apollo-server": "^3.1.2",
"apollo-server-express": "^3.1.2",
"graphql": "^15.5.1",
"graphql-tag": "^2.12.5",
"graphql-tools": "^8.1.0",
"type-graphql": "^1.1.1"
}
I ended up adding metrics inside the reference resolver, which is configured in the schema like the following:
import { addResolversToSchema } from 'apollo-graphql';
function resolveUserReference() {
// Resolve User + metrics
}
const referenceResolvers = {
User: { __resolveReference: resolveUserReference },
};
addResolversToSchema(yourFederatedSchema, referenceResolvers);
When I run sls deploy, I get the following error
Serverless: Bundling with Webpack...
ERROR in /Users/pgonzalez/myapps/happy-soup-bulk-service/node_modules/sfdc-happy-api/lib/rest.js 258:54
Module parse failed: Unexpected token (258:54)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
...
| function isAccessError(jsonResponse){
|
> if(Array.isArray(jsonResponse) && jsonResponse[0]?.message == `Cannot retrieve documents in a user's private folder; move the document to a named folder`){
| return true;
| }
The arrow points to the call of Array.isArray and that is line 258 of the external module, which is the line referenced in the error.
If I remove the import of the external module, the error goes away
import bulkDependecy from 'sfdc-dependency-api-bulk';
What's weird is that I can use Array.isArray() directly on the handler
//import bulkDependecy from 'sfdc-dependency-api-bulk';
async function getUsage(event, context) {
const {ids,connection} = event.body;
// THIS DOESN'T FAIL
Array.isArray(['true']);
//let result = await bulkDependecy.getUsage(ids,connection);
return {
statusCode: 200,
body: 'result'
};
}
export const handler = getUsage;
Here's my serverless.yml
service:
name: happy-soup-bulk-service
plugins:
- serverless-bundle
- serverless-pseudo-parameters
provider:
name: aws
runtime: nodejs12.x
memorySize: 256
stage: ${opt:stage, 'dev'}
region: eu-west-1
functions:
createAuction:
handler: src/handlers/getUsage.handler
events:
- http:
method: POST
path: /usage
And my package.json
{
"name": "happy-soup-bulk-service",
"version": "1.0.0",
"description": "Codingly template for Serverless Framework projects",
"author": "Ariel Weinberger <ariel#codingly.io>",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.0.15",
"babel-polyfill": "^6.23.0",
"babel-preset-env": "^1.6.0",
"serverless-bundle": "^1.3.3",
"serverless-pseudo-parameters": "^2.5.0",
"webpack": "^4.35.2"
},
"dependencies": {
"aws-sdk": "^2.639.0",
"sfdc-dependency-api-bulk": "1.0.0"
}
}
I have a sample notebook app that works with ember-cli's HTTP mocks and also my rails backend using ActiveModelSerializer.
When I hook it to firebase with ember-fire, I am able to register a user (I see it in the dashboard) but when I try to retrieve it by email, I get the following warning:
WARNING: Encountered "0" in payload, but no model was found for model name "0" (resolved model name using ui#serializer:application:.modelNameFromPayloadKey("0"))
then this error:
Error: Assertion Failed: You must include an 'id' for undefined in an object passed to 'push'
at new Error (native)
at Error.EmberError (http://localhost:4200/assets/vendor.js:24735:21)
at assert (http://localhost:4200/assets/vendor.js:14636:13)
at Object.assert (http://localhost:4200/assets/vendor.js:22037:34)
at ember$data$lib$system$store$$Service.extend._pushInternalModel (http://localhost:4200/assets/vendor.js:75316:15)
at ember$data$lib$system$store$$Service.extend.push [as _super] (http://localhost:4200/assets/vendor.js:75302:34)
at push (http://localhost:4200/assets/vendor.js:94940:38)
at superWrapper [as push] (http://localhost:4200/assets/vendor.js:30984:22)
at http://localhost:4200/assets/vendor.js:70210:27
at Object.Backburner.run (http://localhost:4200/assets/vendor.js:9707:25)
I am querying the store using:
export default Ember.Route.extend({
actions: {
login: function() {
this.store.query('user', {
email: this.controller.get('email')
}).then((users) => {
if(users.get('length') === 1) {
var user = users.objectAt(0);
this.controllerFor('application').set('user',user);
this.transitionTo('notebooks', user.get('id'));
}
else {
console.log('unexpected query result');
}
});
}
}
});
Digging in, I can see by setting breakpoint at finders.js#157 I am about to
store._adapterRun(function () {
var payload = normalizeResponseHelper(serializer, store, typeClass, adapterPayload, null, 'query');
//TODO Optimize
records = store.push(payload);
});
push the payload. The adapter payload inspects to
adapterPayload: Array[1]
0: Object
email: "test#test.com"
first_name: "Test"
id: "-K1oINClDw2ylQLww7-p"
last_name: "User"
which is my user. So all's good except for the trace. Not sure about that ID but I am new to firebase; maybe it's ok. It matches what I see in my dashboard.
I haven't done anything special with my serializer -- it's vanilla.
import DS from 'ember-data';
export default DS.RESTSerializer.extend({
});
AFAIK I am using the latest & greatest -- here's bower.json
{
"name": "ui",
"dependencies": {
"ember": "2.1.0",
"ember-cli-shims": "ember-cli/ember-cli-shims#0.0.4",
"ember-cli-test-loader": "ember-cli-test-loader#0.1.3",
"ember-data": "2.1.0",
"ember-load-initializers": "ember-cli/ember-load-initializers#0.1.7",
"ember-qunit": "0.4.9",
"ember-qunit-notifications": "0.0.7",
"ember-resolver": "~0.1.18",
"jquery": "^2.1.4",
"loader.js": "ember-cli/loader.js#3.2.1",
"qunit": "~1.18.0",
"foundation": "~5.5.3",
"bootstrap": "~3.3.5",
"showdown": "~1.3.0",
"firebase": "^2.1.0"
}
}
and my dev dependencies in package.json
"devDependencies": {
"body-parser": "^1.14.1",
"broccoli-asset-rev": "^2.1.2",
"ember-cli": "1.13.8",
"ember-cli-app-version": "1.0.0",
"ember-cli-babel": "^5.1.3",
"ember-cli-dependency-checker": "^1.0.1",
"ember-cli-htmlbars": "1.0.1",
"ember-cli-htmlbars-inline-precompile": "^0.3.1",
"ember-cli-ic-ajax": "0.2.1",
"ember-cli-inject-live-reload": "^1.3.1",
"ember-cli-qunit": "^1.0.0",
"ember-cli-rails-addon": "0.0.12",
"ember-cli-release": "0.2.3",
"ember-cli-showdown": "2.5.0",
"ember-cli-sri": "^1.0.3",
"ember-cli-uglify": "^1.2.0",
"ember-data": "2.1.0",
"ember-disable-proxy-controllers": "^1.0.0",
"ember-export-application-global": "^1.0.3",
"emberfire": "1.6.0",
"express": "^4.13.3",
"glob": "^4.5.3",
"morgan": "^1.6.1",
"nedb": "^1.2.1"
}
Any pointers/help/guidance would be great! I am also new to ember too, so maybe I am missing the obvious?
You need a type key to be returned from your backend, rather than just sending an array. This lets Ember Data know what type of model you're pushing to the store. Your payload should look like this:
users: [
{
email: "test#test.com",
id: "abcdefg"
(...)
}
]
instead of this
[
{
email: "test#test.com",
id: "abcdefg"
(...)
}
]
Alternatively you can explicitly pass in the type and data:
store.push("user", store.normalize("user", response[0]));
(where response is still the array)
Im fairly new to nodejs, and Ive been suffering trying to solve this, and so far I cant find the problem.
At some point during my application I render a EJS template with some parameters, but it seems that even though the parameters are obtained, they are empty....
I tested the most basic case:
console.log(req.user);
res.render('menu.ejs', { user: String(req.user.username) });
the console outputs:
{ id: 1,
username: 'bob',
password: 'secret',
email: 'bob#example.com' }
So Im not sending an empty value.
The menu.ejs code is the following:
<html>
<body>
<% user %>
</body>
</html>
Really simple. But when the page is rendered all I get is an empty page... Even with a and some more HTML, I just keep getting and empty value for user...
At some point in my application I used passport to authenticate my user, and using flash, I managed to get a message through:
res.render('login.ejs', { message: req.flash('message') });
But that still leaves me with my menu.ejs error. Ive renamed the file, renamed the parameter passed, added more parameters, harcoded the values of the parameters passed... and I always get same the results =(
Project Dependencies:
"dependencies": {
"express": "~4.9.x",
"express-session": "1.9.3",
"mysql ": "2.5.3",
"body-parser": "1.10.0",
"passport": "0.2.1",
"passport-local": "1.0.0",
"connect-flash": "0.1.1",
"cookie-parser": "1.3.3",
"ejs": "1.0.0"
}
Any ideas?
Try to change
<% user %>
to
<%= user %>
since that is the usual syntax to output a template variable as far as I know.
I am trying a zappa node application and am unable to get it working beyond a trivial example.
I have application.coffee
require('zappajs') ->
#set views: "#{__dirname}/views"
#set 'view engine': 'jade'
#use 'bodyParser', 'methodOverride', #app.router, 'static'
#configure
development: => #use errorHandler: {dumpExceptions: on}
production: => #use 'errorHandler'
#get '/': -> #render 'index'
and i am getting an error. Zappa seems to be prepending a guid to the file name.
example/app/views/7b16eeab-de6f-41b0-b196-8f23f4b7237d/index.jade
Here is the complete trace
Error: ENOENT, no such file or directory '/Users/irfn/project/example/app/views/7b16eeab-de6f-41b0-b196-8f23f4b7237d/index.jade'
at Object.fs.openSync (fs.js:338:18)
at Object.fs.readFileSync (fs.js:182:15)
at Object.rethrow (/Users/irfn/project/example/node_modules/jade/lib/runtime.js:155:27)
at parse (/Users/irfn/project/example/node_modules/jade/lib/jade.js:116:13)
at Object.exports.compile (/Users/irfn/project/example/node_modules/jade/lib/jade.js:163:9)
at Function.exports.compile (/Users/irfn/project/example/node_modules/zappajs/node_modules/express/lib/view.js:68:33)
at ServerResponse.res._render (/Users/irfn/project/example/node_modules/zappajs/node_modules/express/lib/view.js:417:18)
at ServerResponse.res.render (/Users/irfn/project/example/node_modules/zappajs/node_modules/express/lib/view.js:318:17)
at Object.zappa.app.app.(anonymous function).apply.concat.render (/Users/irfn/project/example/node_modules/zappajs/lib/zappa.js:593:33)
at Object.zappa.app.app.(anonymous function).apply.concat.ctx.render (/Users/irfn/project/example/node_modules/zappajs/lib/zappa.js:545:31)
Here is how my package json dependencies are setup
"dependencies": {
"coffee-script": "1.3.3",
"underscore": ">= 1.3.3",
"stylus": ">= 0.28.2",
"zappajs": ">= 0.3.10",
"jade": ">= 0.18.0"
},
"devDependencies": {
"watchr": "*",
"mocha": "*",
"chai": "*",
"request": "*"
},
Came across this myself - did not have the time to track down the cause but there is an easy workaround:
instead of
#get / : ->
#render index: {key:value}
use the more old school connect-style
#app.get '/', (req,res) ->
res.render 'index', {key:value}
Hope this helps you get over the hump.
PS:
Come to think of it, I assume it might have something to do with the way zappa evaluates coffee(k/c)up templates by default. In this case it might be trying to apply that logic to jade which breaks.