bookshelf-modelbase - Unexpected token in findOne method - bookshelf.js

I'm trying bookshelf-modelbase and getting a SyntaxError while running the server:
//model.js
var knex = require('knex')(require('../knexfile').development);
var bookshelf = require('bookshelf')(knex);
var ModelBase = require('bookshelf-modelbase')(bookshelf);
//...
And the error:
/node_modules/bookshelf-modelbase/lib/index.js:77
return this.findOne({ [this.prototype.idAttribute]: id }, options)
^
SyntaxError: Unexpected token [
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
Has anyone ever had this error?

You should try Node.js v4 and higher.

try this:
var param = {}
param[this.prototype.idAttribute]=id
return this.findOne(param, options)

Related

Firebase function failing - Unexpected identifier: initializeIfNeeded

I have deployed the following firebase function:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
// other functions
exports.createProfileDocument = functions.auth.user().onCreate(async user => {
await admin.firestore().collection('profiles').doc(user.uid).set({
userName: user.displayName
})
});
This was working but recently stopped, when a new user account was created the function would simply not fire (nothing in the logs, no errors, no activity, etc.). I updated my dependencies to the following:
"dependencies": {
"firebase-admin": "^8.0.0",
"firebase-functions": "^2.3.1",
"firebase-tools": "^6.11.0"
},
The function is now firing when I would expect but fails with the following error:
/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/build/src/index.js:740
async initializeIfNeeded() {
^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at FirebaseNamespace.get [as firestore] (/user_code/node_modules/firebase-admin/lib/firebase-namespace.js:329:29)
Any ideas?
It looks as though this was failing due to the async keyword before initializeIfNeeded() as this is not available in node 6. I was able to resolve the issue by adding
"engines": { "node": "8" }
To my package.json
You need to update your node to version 8.x, which allows async hooks.
Upgrade: How to update nodejs from 6.x to 8.x?

Sendgrid import issue in Meteor

I'm trying to use sendgrid npm package in Meteor (on the server):
const sendgridMail = require('#sendgrid/mail');
Keep getting this error:
(STDERR) packages\modules.js:961
(STDERR) const {
(STDERR) ^
(STDERR)
(STDERR) SyntaxError: Unexpected token {
(STDERR) at Object.exports.runInThisContext (vm.js:53:16)
(STDERR) at D:\myProject\.meteor\local\build\programs\server\boot.js:331:30
(STDERR) at Array.forEach (native)
(STDERR) at Function._.each._.forEach (C:\Users\user1\AppData\Local\.meteor\packages\meteor-tool\1.5.2\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\underscore\underscore.js:79:11)
Any ideas how to fix it?
Update: package.json includes the following dependencies:
"dependencies": {
"#sendgrid/client": "^6.1.4",
"#sendgrid/mail": "^6.1.4",
"babel-runtime": "^6.20.0",
"bcrypt": "^1.0.2",
"body-parser": "^1.17.2",
"card": "^2.3.0",
"google-auth-library": "^0.10.0",
"googleapis": "^21.3.0",
"meteor-node-stubs": "~0.2.4",
"moment": "^2.18.1",
"pnotify": "^3.2.0",
"shortid": "^2.2.8",
"simpl-schema": "^0.3.1",
"stripe": "^4.24.0"
}
I use it like this, and find it works fine.
import sendgridModule from 'sendgrid';
let SEND_GRID_API_KEY = '';
try {
SEND_GRID_API_KEY = Meteor.settings.env.SEND_GRID_API_KEY;
} catch (e) {
// no-op
}
const sendgrid = sendgridModule(SEND_GRID_API_KEY);
I think using import instead of require is preferred, and it now can be used for conditional imports
The problem is that SendGrid SDK v6 requires Node.js version 6 and higher, but the one bundled in Meteor is 4.8.4:
$ meteor node --version
v4.8.4
As stated in this issue, updating Node.js will help, but it's obviously can't be done with Meteor.
I suggest you to use sendgrid npm package, this one works fine with Node.js v4.

Meteor 1.3 update migration

I'm just working through the update to 1.3 and not sure how to handle this error. I'm thinking it might have to do with file load order changing in 1.3. Any ideas? Thanks!
W20160407-09:54:43.528(1)? (STDERR) /Users/technical/.meteor/packages/meteor-tool/.1.3.1.10rlef4++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:267
W20160407-09:54:43.528(1)? (STDERR) throw(ex);
W20160407-09:54:43.528(1)? (STDERR) ^
W20160407-09:54:43.553(1)? (STDERR) TypeError: Cannot read property 'path' of undefined
W20160407-09:54:43.553(1)? (STDERR) at Routing (packages/lookback:emails/lib/routing.js:17:9)
W20160407-09:54:43.554(1)? (STDERR) at packages/lookback:emails/lib/mailer.js:279:11
W20160407-09:54:43.554(1)? (STDERR) at Array.forEach (native)
W20160407-09:54:43.554(1)? (STDERR) at packages/lookback:emails/lib/mailer.js:278:28
W20160407-09:54:43.554(1)? (STDERR) at Function._.each._.forEach (packages/underscore.js:147:22)
W20160407-09:54:43.554(1)? (STDERR) at Object.init (packages/lookback:emails/lib/mailer.js:274:9)
W20160407-09:54:43.554(1)? (STDERR) at Object.Mailer.init (packages/lookback:emails/lib/mailer.js:303:7)
W20160407-09:54:43.554(1)? (STDERR) at app/server/lib/config/config.js:72:8
W20160407-09:54:43.554(1)? (STDERR) at /Users/technical/code/mssc1.3/.meteor/local/build/programs/server/boot.js:290:5
server/lib/config/config.js
Meteor.startup(function() {
this.Templates = {}
Templates.remindEventEmail = {
path: 'remindEventEmail.html'
};
Mailer.init({
templates: Templates
});
});
private/remindEventEmail.html
<p>Email code<p>
it's not a 1.3 thing, it's a lookback > 0.7.0 thing
if you don't need email previews, simply setting
Mailer.config({
addRoutes: false
});
may fix your problem.
otherwise follow along for how I use it with Flow Router
according to:
you now need to supply a route field on your template object
https://github.com/lookback/meteor-emails#version-history
route: {
path: '/sample/:name',
// params is an object, with potentially named parameters, and a `query` property
// for a potential query string.
data: function(params) {
// `this` is the HTTP response object.
return {
name: params.name // instead of this.params.name
};
}
}
so your
Templates.remindEventEmail = {
path: 'remindEventEmail.html'
};
should become
Templates.remindEventEmail = {
path: 'remindEventEmail.html'
route: {
path: '/event/:_id/remind',
data: function(params) {
return {
event: Events.findOne(params._id);
}
}
}
};
the actual route would then be /emails/event/:_id/remind
please notice the data pattern, you might use another style of query or nesting of the main data context

Meteor: ReferenceError: Accounts is not defined

I just finished developing a Meteor package. Now I want to test it by adding it to a new Meteor app:
my_cool_package_name/package.js
Package.on_use(function(api){
api.use("accounts-password#1.1.1");
});
Add my_cool_package_name to a new project
meteor add my_cool_package_name
Changes to your project's package version selections:
accounts-base added, version 1.2.0
accounts-password added, version 1.1.1
List installed packages
meteor list
meteor-platform 1.2.2 Include a standard set of Meteor packages in your app
my_cool_package_name 1.0.0+ This is my_cool_package_name
Start meteor
meteor
ReferenceError: Accounts is not defined
W20150817-15:30:49.707(-4)? (STDERR) at manage_users_log.insert.username (app/server/fixtures.js:7:17)
W20150817-15:30:49.707(-4)? (STDERR) at Array.forEach (native)
W20150817-15:30:49.707(-4)? (STDERR) at Function._.each._.forEach (packages/underscore/underscore.js:105:1)
W20150817-15:30:49.708(-4)? (STDERR) at app/server/fixtures.js:6:4
W20150817-15:30:49.708(-4)? (STDERR) at app/server/fixtures.js:36:3
W20150817-15:30:49.708(-4)? (STDERR) at /Users/me/Documents/meteor/my_app/.meteor/local/build/programs/server/boot.js:222:10
W20150817-15:30:49.708(-4)? (STDERR) at Array.forEach (native)
W20150817-15:30:49.708(-4)? (STDERR) at Function._.each._.forEach (/Users/me/.meteor/packages/meteor-tool/.1.1.4.1kp2n64++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
W20150817-15:30:49.708(-4)? (STDERR) at /Users/me/Documents/meteor/my_app/.meteor/local/build/programs/server/boot.js:117:5
=> Exited with code: 8
=> Your application is crashing. Waiting for file change.
server/fixtures.js
if (Meteor.users.find().count() === 0) {
var users = [
{username:'admin',email:'admin#example.com',password:'adminadmin',roles:['admin'],status:'enabled',profile:{first_name:'admin',last_name:'admin'}},
{username:'user',email:'user#example.com',password:'useruser',roles:['user'],status:'enabled',profile:{first_name:'user',last_name:'user'}}
];
_.each(users, function(user){
var user_id = Accounts.createUser({
username: user.username,
email: user.email,
password: user.password,
profile: {
first_name: user.profile.first_name,
last_name: user.profile.last_name,
}
});
Meteor.users.update(
{_id: user_id},
{$set:
{
roles: user.roles,
status: user.status
}
}
);
});
}
if (manage_users_log.find().count() === 0) {
manage_users_log.insert({
username: "admin",
category: "server_startup",
description: "Meteor server started."
});
}
If it helps, the app structure is laid out like so:
client
my_app.html
packages
my_cool_package_name
server
fixtures.js
Use api.imply to give your app access to the exported symbols of your package dependencies.
Package.onUse(function(api){
api.use("accounts-password#1.1.1");
//
api.imply("accounts-password#1.1.1");
});
When you add a package depending on other packages, their exported symbols are not available to the main app, you need to imply them to make this happen.

how to use child_process.fork in meteor

I test the code in meteor .There are some errors.
/server/lib/Test.js
Test = {
say:function(){
console.log("hello world");
}
}
/server/main.js
Meteor.startup(function(){
Test.say(); //work well
var path = Npm.require('path');
var base = path.resolve('.');
var n = Npm.require('child_process').fork(base+"/app/server/children.js");
n.on('message', function(m) {
console.log('PARENT got message:', m);
});
n.send({ hello: 'world' });
});
/server/children.js
process.on('message', function(m) {
Test.say(); //throw a error . The Test is undefined
console.log('CHILD got message:', m);
process.send({name:"ABC"});
});
process.send({ foo: 'bar' });
errors:
I20131223-16:34:44.717(8)? hello world
W20131223-16:34:44.784(8)? (STDERR) /home/ec/workspace/meteor/testChildrenProcess/.meteor/local/build/programs/server/app/server/children.js:2
W20131223-16:34:44.784(8)? (STDERR) Test.say();
W20131223-16:34:44.785(8)? (STDERR) ^
W20131223-16:34:44.785(8)? (STDERR) ReferenceError: Test is not defined
W20131223-16:34:44.787(8)? (STDERR) at process. (/home/ec/workspace/meteor/testChildrenProcess/.meteor/local/build/programs/server/app/server/children.js:2:3)
W20131223-16:34:44.788(8)? (STDERR) at process.EventEmitter.emit (events.js:98:17)
W20131223-16:34:44.788(8)? (STDERR) at handleMessage (child_process.js:318:10)
W20131223-16:34:44.788(8)? (STDERR) at Pipe.channel.onread (child_process.js:345:11)
The Test Object is undefined in "/app/server/children.js" bu it's normal in main.js.
Why? Have I forgotten anything? Any help is appreciated.
The variables are scoped. While in Meteor Test would be available to the other files in your meteor project they are not available to Npm modules/stuff required by Npm.require.
The children.js would be in a different namespace to your main.js file. To access the Test variable in it you would need to pass it a param or as a message.

Resources