ORIGINAL QUESTION -- HAS BEEN FIXED (see below)
I'm new to meteor and trying to use the meteor-balanced-payments package by Ian Serlin (https://github.com/ianserlin/meteor-balanced-payments). The package appears properly installed, yet I'm having problems on both the client and server side just to properly load the package to start using it. I could use a complete running example if anyone has one.
The client side looks like the underscore builtin package is being loaded after the session-extras package that meteor-balanced-payments advocates for using. Note that Meteor.settings.public.balanced.marketplaceUri is set correctly to my test marketplace Uri string of the form /v1/marketplaces/TEST-########################. The client init code is:
[client/main.js]:
Meteor.startup(function() {
Session.whenTrue("balancedLoaded", function() {
balanced.init(Meteor.settings.public.balanced.marketplaceUri);
});
});
When run the following errors appear in the Chrome javascript console. The first line of session-extras.js is "_.extend(Session,{", which means the problem is the first "_" underscore use. Any suggestions what to do? Don't the packages make the dependencies explicit? I'm not sure what to do to correct.
Uncaught ReferenceError: _ is not defined session-extras.js:1
Uncaught TypeError: Object [object Object] has no method 'whenTrue'
The second problem is the server side fails claiming that nbalanced does not exist. The code for the server and the corresponding console errors are as follows. This looks to be just how the meteor-balanced-payments package docs suggest it to be done. nbalanced is in the smart package as a Npm dependency that gets built as part of the package. Any suggestions why nbalanced is not defined?
[server/bootstrap.js]:
Meteor.startup(function () {
balanced = new nbalanced({
secret: Meteor.settings.balanced.apiSecret,
marketplace_uri: Meteor.settings.balanced.marketplaceUri
});
});
console output:
2042-01:58:33.453(-4)? (STDERR) /Users/foo/test-payments4/.meteor/local/build/programs/server/boot.js:185
W2042-01:58:33.454(-4)? (STDERR) }).run();
W2042-01:58:33.454(-4)? (STDERR) ^
W2042-01:58:33.456(-4)? (STDERR) ReferenceError: nbalanced is not defined
W2042-01:58:33.457(-4)? (STDERR) at app/server/bootstrap.js:12:18
W2042-01:58:33.457(-4)? (STDERR) at /Users/foo/test-payments4/.meteor/local/build/programs/server/boot.js:158:61
W2042-01:58:33.457(-4)? (STDERR) at Array.forEach (native)
W2042-01:58:33.457(-4)? (STDERR) at Function._.each._.forEach (/Users/foo/.meteor/tools/a80b2d5689/lib/node_modules/underscore/underscore.js:79:11)
W2042-01:58:33.458(-4)? (STDERR) at /Users/foo/test-payments4/.meteor/local/build/programs/server/boot.js:158:5
=> Exited with code: 8
Other relevant files ...
[smart.json]:
{
"packages": {
"balanced-payments": {},
"session-extras": {},
"sync-methods": {}
}
}
[smart.lock]:
{
"meteor": {},
"dependencies": {
"basePackages": {
"balanced-payments": {},
"session-extras": {},
"sync-methods": {}
},
"packages": {
"balanced-payments": {
"git": "https://github.com/ianserlin/meteor-balanced-payments.git",
"tag": "v0.1.1",
"commit": "6ebc8f9855f35c040b67f0d9bebf16870160c1b2"
},
"session-extras": {
"git": "https://github.com/belisarius222/meteor-session-extras.git",
"tag": "v0.3.0",
"commit": "0c49d41009821e76f46af3ad65d0a1d5bfa8d4eb"
},
"sync-methods": {
"git": "https://github.com/ianserlin/meteor-sync-methods.git",
"tag": "v0.1.1",
"commit": "2a7b094d54f3fb7c9acebbe10b68f9db1e5f156b"
}
}
}
}
Relevant version numbers:
$ uname -a
Darwin mini-en0.home 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64
$ node --version
v0.10.19
$ mrt --version
Meteorite version 0.6.14
SOLUTION
The problem is that both of these packages (balanced-payments and session-extras) have not been updated for the 0.6.5 namespace changes. I made the following changes to a local copy of the packages to get them to work. Now it runs cleanly.
[balanced-payments/package.js]: Needs to export the nbalanced symbol with api.export() so the server can use it ...
Package.describe({
summary: 'Balanced Payments (nbalanced packaged for meteor)'
});
Npm.depends({
'nbalanced': 'https://github.com/ianserlin/nbalanced/tarball/05eb18cf3536e22b62f349d0520e5df23740dd5c'
});
Package.on_use(function (api) {
api.use('sync-methods', 'server');
api.add_files('index.js', 'server');
api.add_files('balanced.js', 'client');
api.export('nbalanced', 'server');
});
[session-extras/package.js]: Needs to import underscore and session via api.use() so that Session can be extended ...
Package.describe({
summary: "a few useful helpers for Meteor's Session"
});
Package.on_use(function (api) {
api.use('underscore', 'client');
api.use('session', 'client');
api.add_files([
'session-extras.js'
],'client');
});
Related
Need to know:
I'm using the latest version of meteor
I'm using the latest version of moment.js
This code is on the server side
var date = moment(new Date());
console.log(date.format("YYYY-MM-DD HH:mm:ss.SS"));
I end up with this error
SyncedCron: Exception "count daily parks per spot" ReferenceError: moment is not defined
at spotIDList.forEach (app/server/main.js:66:18)
at SynchronousCursor.forEach (packages/mongo/mongo_driver.js:1054:16)
at Cursor.(anonymous function) [as forEach] (packages/mongo/mongo_driver.js:876:44)
at Object.job (app/server/main.js:62:16)
at packages/percolate_synced-cron.js:242:26
at scheduleTimeout (packages/percolate_synced-cron.js:290:7)
at packages/percolate_synced-cron.js:338:42
at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1186:26)
at packages/meteor.js:502:25
at runWithEnvironment (packages/meteor.js:1238:24)
the snippet above comes from the following code
SyncedCron.add({
name: 'Simple Cron Function',
schedule: function(parser) {
// parser is a later.parse object
return parser.text('every 15 second');
},
job: function(intendedAt) {
var spotList = spots.find({});
spotList.forEach((spot) => {
var devaddr = `${spot.devaddr}`;
var date = moment(new Date());
console.log(date.format("YYYY-MM-DD HH:mm:ss.SS"));
var dailyVisitsCount = log.find(
{
devaddr : devaddr,
car : "1",
createdAt: {
$gte: date.format("YYYY-MM-DD HH:mm:ss.SS")
}
},
{
sort: {
createdAt:1
}
}
).count();
spots.update({devaddr : devaddr}, { $set: {numberOfParksToday: dailyVisitsCount } } , {multi: false}, function(err, res) {
if (err) throw err;
});
console.log("Daily Visits Count Updated to " + dailyVisitsCount);
});
}
});
Any insight to why this block of code is not working the way it should that would be appreciates.
I did find out that this problem occurred due to a bug in a previous release of meteor which has been fixed.
I'm just trying to get today's date in a format my DB accepts and use that to filter my results. alternative suggestions is also welcomed.
PS: Momnet.js works for me in different locations(client side code)
EDIT:
W20180312-08:03:15.324(-7)? (STDERR) app/server/main.js:1
W20180312-08:03:15.325(-7)? (STDERR) (function(Npm,Assets){(function(){import moment from 'moment'
W20180312-08:03:15.325(-7)? (STDERR) ^^^^^^
W20180312-08:03:15.325(-7)? (STDERR)
W20180312-08:03:15.325(-7)? (STDERR) SyntaxError: Unexpected reserved word
W20180312-08:03:15.326(-7)? (STDERR) at Object.exports.runInThisContext (vm.js:53:16)
W20180312-08:03:15.327(-7)? (STDERR) at /home/elder/Desktop/Recreation-of-ParkeyeDashboard/ParkeyeDashboard/.meteor/local/build/programs/server/boot.js:289:30
W20180312-08:03:15.327(-7)? (STDERR) at Array.forEach (native)
W20180312-08:03:15.329(-7)? (STDERR) at Function._.each._.forEach (/home/elder/.meteor/packages/meteor-tool/.1.4.2_3.14963jl++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
W20180312-08:03:15.330(-7)? (STDERR) at /home/elder/Desktop/Recreation-of-ParkeyeDashboard/ParkeyeDashboard/.meteor/local/build/programs/server/boot.js:128:5
W20180312-08:03:15.330(-7)? (STDERR) at /home/elder/Desktop/Recreation-of-ParkeyeDashboard/ParkeyeDashboard/.meteor/local/build/programs/server/boot.js:344:5
W20180312-08:03:15.330(-7)? (STDERR) at Function.run (/home/elder/Desktop/Recreation-of-ParkeyeDashboard/ParkeyeDashboard/.meteor/local/build/programs/server/profile.js:480:12)
W20180312-08:03:15.331(-7)? (STDERR) at /home/elder/Desktop/Recreation-of-ParkeyeDashboard/ParkeyeDashboard/.meteor/local/build/programs/server/boot.js:343:11
You need to install the moment.js package:
meteor npm i --save moment
Then at the top of your file you need to import moment:
import moment from 'moment'
Then to get the date in a format the DB will accept using moment just do:
var date = moment().toDate()
Using moment() creates a moment object of the current date/time. Running toDate() on it gives you the same date/time in a Date object.
I have a function in Firebase Functions service that send any FCM.
I would to use admin.messaging().send() function, like this reference guide, but I got this error while function is triggered, not during deploy:
TypeError: admin.messaging(...).send is not a function
at exports.sendChatNotification.functions.database.ref.onCreate.event (/user_code/lib/index.js:113:30)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36)
at /var/tmp/worker/worker.js:700:26
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
I can see this error in Functions->Log inside Firebase console.
That's my function code:
exports.sendChatNotification = functions.database.ref('/messages').onCreate(event => {
var message = {
data: {
title: 'title',
body: 'body',
},
apns: {
header: {
'apns-priority': '10',
'apns-expiration':'0'
},
payload: {
aps: {
sound: 'default',
'content-available':'1'
}
}
},
android: {
ttl: 60*1000,
priority: 'high'
},
topic: 'mytopic'
};
return admin.messaging().send(message);
});
If I use admin.messaging().sendToTopic() and (changing the message structure) it works fine. It seems Firebase doesn't support its own API.
I deploy this using Firebase tools, with command line "firebase deploy".
I have updated firebase-tools and firebase-functions and firebase-admin in my functions project.
The send() function was added in firebase-admin 5.9.0. If you want to use it, you should run npm install firebase-admin#latest in your functions folder to install the latest version. At the time of this writing, the latest version is 5.9.1.
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.
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.
In a NodeJS application ,I have finished some modules,Now I want to use them in Meteor,What should I do?
For example,there is a file 'hello.js',content:
require('url');// In here,require other modules
function sayHi(name){
console.log("Hi "+ name);
}
exports.sayHi = sayHi;
How do I use 'say Hi' in meteor?
when I do this:
if (Meteor.isServer) {
Meteor.startup(function () {
var require = __meteor_bootstrap__.require;
var index = require('./hello');
hello.syaHi('Ec');})}
Errors is:
app/index.js:1
require();
^
ReferenceError: require is not defined
at app/index.js:1:1
at /home/huyinghuan/workspace/NodeJs/myMeteorJS/testrequire/.meteor/local/build/server/server.js:113:21
at Array.forEach (native)
at Function._.each._.forEach (/usr/lib/meteor/lib/node_modules/underscore/underscore.js:79:11)
at run (/home/huyinghuan/workspace/NodeJs/myMeteorJS/testrequire/.meteor/local/build/server/server.js:99:7)
I think, you have to install/copy your module into projectdir/.meteor/local/build/server/node_modules which is a link to /usr/local/meteor/lib/node_modules. I tried this with the node.js module tracer and it worked. You have to copy your files into this directory every time you updated your meteor installation.
Also, it looks like Npm.require() is the right way to require node modules now.
Update, I had to install my module into .meteor/local/build/programs/server/node_modules as well as use Npm.require.
Here's a package which makes it a lot easier to use NPM packages inside Meteor:
https://github.com/meteorhacks/npm
example usage:
if (Meteor.isClient) {
getGists = function getGists(user, callback) {
Meteor.call('getGists', user, callback);
}
}
if (Meteor.isServer) {
Meteor.methods({
'getGists': function getGists(user) {
var GithubApi = Meteor.npmRequire('github');
var github = new GithubApi({
version: "3.0.0"
});
var gists = Async.runSync(function(done) {
github.gists.getFromUser({user: 'arunoda'}, function(err, data) {
done(null, data);
});
});
return gists.result;
}
});
}