Realm-js throws exception - javascript

I'm trying to write a little test of realm-js using node.js console, but after insertion of te first string I see an exception:
> var Realm = require('realm')
TypeError: utf8 is not a function
at Function.from (native)
at Function.from (native)
at module.exports (/home/hermann/node_modules/realm/lib/submit-analytics.js:92:60)
at Object.<anonymous> (/home/hermann/node_modules/realm/lib/index.js:70:42)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
Anybody can help me with this problem?
UPD. An error occures in realm/lib/submit-analytics.js in this call:
request(`https://api.mixpanel.com/track/?data=${Buffer.from(JSON.stringify(payload), 'utf8').toString('base64')}&ip=1`,
() => { /* Analytics failed. Do nothing. */ });

That's a bug, you must be using Node.js 4.x? I've created an issue to track this -- for now you can just comment out that entire line to make it work for you.

Related

TypeError: createDebug.load is not a function

I am facing the following error while trying to serve my project built using AdonisJs.
Error:
at setup (D:\NodeJsProjects\AdonisWallet\node_modules\knex\node_modules\debug\src\common.js:261:33)
at Object.<anonymous> (D:\NodeJsProjects\AdonisWallet\node_modules\knex\node_modules\debug\src\node.js:236:37)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (D:\NodeJsProjects\AdonisWallet\node_modules\knex\node_modules\debug\src\index.js:9:19)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
This problem is caused by the following line of code. In this code, AddonModel is a lucid Model provided by adonisjs
const ad= await AddonModule.findBy('name', addon_name)
I am not sure why is this happening. I have properly imported model using const AddonModule = use('App/Models/Addon')
Code seems like this
await files.forEach(async (file) => {
var addon_name=file
const ad= await AddonModule.findBy('name', addon_name)
})

google-spreadsheet.js (npm) is not working when follow the github example

When following the basic exmaple code from https://github.com/theoephraim/node-google-spreadsheet, it's not working.
for example:
const { GoogleSpreadsheet } = require('google-spreadsheet')
const creds = require('./gsheet-creds.json')
const doc = new GoogleSpreadsheet('1nkNX-HfxYY5qs_MwXTiFQIxEajibJlTTUPeOHCX7ZVc')
doc.useServiceAccountAuth({
client_email: creds.client_email,
private_key: creds.private_key,
})
await doc.loadInfo()
console.log(doc.title)
it shows error:
sz#air:/mnt/d/coding/wxBot-dorm-seller/googlesheet$ node gsheet-google-spreadsheet.js
/mnt/d/coding/wxBot-dorm-seller/googlesheet/gsheet-google-spreadsheet.js:34
await doc.loadInfo()
^^^^^
SyntaxError: await is only valid in async function
at wrapSafe (internal/modules/cjs/loader.js:1053:16)
at Module._compile (internal/modules/cjs/loader.js:1101:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
If remove the 'await' before doc.loadInfo(), it shows error:
sz#air:/mnt/d/coding/wxBot-dorm-seller/googlesheet$ node gsheet-google-spreadsheet.js
gsheet#gsheet-yiouyou.iam.gserviceaccount.com
/mnt/d/coding/wxBot-dorm-seller/googlesheet/node_modules/_google-spreadsheet#3.0.13#google-spreadsheet/lib/GoogleSpreadsheet.js:175
if (!this._rawProperties) throw new Error('You must call `sheet.loadInfo()` before accessing this property');
^
Error: You must call `sheet.loadInfo()` before accessing this property
at GoogleSpreadsheet._ensureInfoLoaded (/mnt/d/coding/wxBot-dorm-seller/googlesheet/node_modules/_google-spreadsheet#3.0.13#google-spreadsheet/lib/GoogleSpreadsheet.js:175:37)
at GoogleSpreadsheet._getProp (/mnt/d/coding/wxBot-dorm-seller/googlesheet/node_modules/_google-spreadsheet#3.0.13#google-spreadsheet/lib/GoogleSpreadsheet.js:192:10)
at GoogleSpreadsheet.get title [as title] (/mnt/d/coding/wxBot-dorm-seller/googlesheet/node_modules/_google-spreadsheet#3.0.13#google-spreadsheet/lib/GoogleSpreadsheet.js:199:29)
at Object.<anonymous> (/mnt/d/coding/wxBot-dorm-seller/googlesheet/gsheet-google-spreadsheet.js:35:17)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
I've tried many things and checked many examples from website and youtube. None of them is working for the latest google-spreadsheet. I haven't try anything older.
Appreciate any help here. Really want to get the google-spreadsheet.js working.
You are using async + await wrong. You should use "await" only when calling a declared "async" function. See this answer for a detailed explanation of the use.
Put the last 2 lines in an asynchronous function.
I think that will help you.
For example:
const loadDoc = async (creds) => {
await doc.loadInfo();
let sheet = doc.sheetsByIndex[0];
console.log(sheet.title);
};
loadDoc(creds);

TypeError: Invalid connection Details (PG-Promise)

So I'm trying to solve an issue that PG-Promise is giving me, but I'm not entirely certain as to why. I'm very certain that I set up the database and environment for it correctly, and have properly seeded and tested the database locally. If needed be, I can link the respective queries I have that make calls to the database, but I'm not sure if that'll help solve this.
That said, below is the pg-promise environment and the exact error it's firing. I appreciate any help with solving this!
const promise = require('bluebird');
promise object has .catch/,then etc..
const options = {
promiseLib: promise
};
const pgp = require('pg-promise')(options);
const connectionString = process.env.DATABASE_URL;
my .env's database url is the following: DATABASE_URL=postgres://localhost:5432/project3//
const db = pgp(connectionString);
db.connect()
.then(obj => {
obj.done();
})
.catch(error => {
console.log('ERROR:', error.message || error);
});
TypeError: Invalid connection details.
at inst (/Users/benjidurden/Documents/projects/mighty-shelf-36224/node_modules/pg-promise/lib/main.js:177:15)
at Object.<anonymous> (/Users/benjidurden/Documents/projects/mighty-shelf-36224/routes/queries2.js:12:12)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/benjidurden/Documents/projects/mighty-shelf-36224/routes/index.js:4:10)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
If you just look at the code, then it is obvious that your process.env.DATABASE_URL is either null or an empty string, neither of which is a valid connection string, hence the error.
my .env's database url is the following: DATABASE_URL=postgres://localhost:5432/project3
You presume so, but I'm betting that it is not.
UPDATE
Following this question, I have refactored the initialization errors in pg-promise, to include the details of what is in fact being passed in, available from v5.7.2.

I cannot use import in Jasmine2 spec when using protractor 4.0.9 when using typescript 2.0

When I try to use typescript .ts spec and run them in protractor, I get:
[17:05:52] E/launcher - Error: SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at C:\Users\gwk736\Gitlab\inform-locate-E2E-tests\node_modules\jasmine\lib\jasmine.js:84:5
at Array.forEach (native)
at Jasmine.loadSpecs (C:\Users\gwk736\Gitlab\inform-locate-E2E-tests\node_modules\jasmine\lib\jasmine.js:83:18)
[17:05:52] E/launcher - Process exited with error code 100
When I have .ts file and no import is present it's behaving ok. Why ?
) Get the same error, using: Jasmine2, Protractor 4.0.11, typescript 2.0.3
And I don't know what seems to be the problem. But my workaround is:
in file: main-page.ts
module.exports = {
setUrl: function(){
browser.get("/");
}
};
and in file you want to use the function setURL for example:
var mainPage = require('./main-page.ts');
describe('Test Main Page', function() {
it('should have a title', function() {
mainPage.setUrl();
expect(browser.getTitle()).toEqual('Titel meiner Seite');
});
});
That's just my workaround - actually I didn't find out the real reason for the problem you described!

Extending Error gives wrong stack trace information

This question is not the same as Extended Errors do not have message or stack trace, which deals with es6 extends, but I'm using es5.
Here's my code
test.js
function FooError(message) {
this.stack = (new Error(message)).stack;
console.log('this.stack', this.stack);
console.log('new_error_stack', (new Error(message)).stack);
console.log('after');
}
FooError.prototype = new Error;
throw new FooError("foo");
This gives the following output : with node version 6.4 (node test.js)
this.stack Error
at Object.<anonymous> (/tmp/test.js:7:22)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
new_error_stack Error: foo
at Error.FooError (/tmp/test.js:4:33)
at Object.<anonymous> (/tmp/test.js:9:7)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
after
/tmp/test.js:9
throw new FooError("foo");
^
Error
at Object.<anonymous> (/tmp/test.js:7:22)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
as you can see, the 'this.stack' doesn't show the same thing at all than the 'new_error_stack', eg, the this.stack shows the stack as if the error was thrown when the require was called. Also, the thrown stack is also "incorrect".
I took the code from https://stackoverflow.com/a/5251506/1993501 , and since I want to use instanceof FooError, I use the line FooError.prototype = new Error.
As you can see, the 'new_error_stack' gives the expected output, but I would have expected both values to be equal.
This has the effect that errors are not very useful for users of my library, see corresponding issue : https://github.com/open-xml-templating/docxtemplater/issues/245
What exactly is happening, and how can I prevent it and keep proper error types (eg being able to use instanceof) ?
I found a possible solution to your problem, but I'm not entirely sure it fixes all of your issues. I'm using Error.prototype instead of new Error (which is what you had). I found this solution from the first answer provided to this question: How do I create a custom Error in JavaScript?
I also changed your console.log lines to concatenate the two strings, as I was using alert() to display the output for testing purposes.
Kevin Hakanson goes over why you may or may not want to go with this solution in his answer. Here's the code to answer your question:
function FooError(message) {
this.stack = (new Error(message)).stack;
console.log('this.stack ' + this.stack);
console.log('new_error_stack ' + (new Error(message)).stack);
console.log('after');
}
FooError.prototype = Error.prototype;
throw new FooError("foo");
You may want to use a try/catch block with that last line.

Categories

Resources