Category Archives: Code

Code is a question and answer site for professional and enthusiast programmers.

NodeMailer – Cannot read property ‘getSocket’ of undefined

Node: v8.6.0
Nodemailer: v4.6.4

This is my code:

const transport = nodemailer.createTransport({ host: process.env.MAIL_HOST, port: process.env.MAIL_PORT, auth: { user: process.env.MAIL_USER, pass: process.env.MAIL_PASS } }); const generateHTML = (filename, options = {}) => { const html = pug.renderFile(`${__dirname}/../views/email/${filename}.pug`, options); const inlined = juice(html); return inlined; } exports.send = async (options) => { const html = generateHTML(options.filename, options); const text = htmlToText.fromString(html); const mailOptions = { from: `Site <noreply@domain.com>`, to: options.user.email, subject: options.subject, html, text }; const sendMail = P.promisify(transport.sendMail, transport); return sendMail(mailOptions); }

When i execute sendMail i get this fail:
TypeError: Cannot read property ‘getSocket’ of undefined↵ at sendMail (/Users/…../node_modules/nodemailer/lib/mailer/index.js:143:24

I check the mention line and is this one:

if (typeof this.getSocket === 'function') {
            this.transporter.getSocket = this.getSocket;
            this.getSocket = false;
        }

NodeMailer – Cannot read property ‘getSocket’ of undefined

read more

A more elegant way of deleting the last value of multiple lists?

I have multiple lists namely, X, Y, VX, VY, R, T, KE, PE, TE, for which I need to delete their (i+1)th value off once their iterations break. The way I can do it currently is if I do:

del X[i+1:]
del Y[i+1:]
del VX[i+1:]
del VY[i+1:]
del R[i+1:]
del T[i+1:]
del KE[i+1:]
del PE[i+1:]
del TE[i+1:]

Which I think it looks rather ugly. Is there a way to minimize the use of lines in this case. Maybe a loop which does that but in just 1 or 2 lines?

Edit:
I didn’t want to include the body of my code because I’m aware that a lot of my colleagues are doing the same problem and I don’t want anyone to plagiarize. Anyway, here’s what I did.

def F_1(X):
    return VX

def F_2(X):
    return VY

def F_3(X, Y):
    return -G*EM*X/((X**2 + Y**2)**(3/2))

def F_4(X, Y):
    return -G*EM*Y/((X**2 + Y**2)**(3/2))

def RKutta(F_1, F_2, F_3, F_4, XSTART, YSTART, VXSTART, VYSTART):
    N = 100000
    X, Y = np.zeros([N+1]), np.zeros([N+1])
    VX, VY = np.zeros([N+1]), np.zeros([N+1])
    R, T =  np.zeros([N+1]), np.zeros([N+1])
    KE, PE, TE = np.zeros([N+1]), np.zeros(N+1), np.zeros([N+1])

    X[0], Y[0] = XSTART, YSTART
    VX[0], VY[0] = VXSTART, VYSTART
    T[0] = 0
#    KE[0], PE[0], TE[0] = KE, PE, TE

    for i in range (1,N):

        K_1X = F_1(VX[i-1])
        K_1Y = F_2(VY[i-1])
        K_1VX = F_3(X[i-1], Y[i-1])
        K_1VY = F_4(X[i-1], Y[i-1])

        K_2X = F_1(VX[i-1] + (H*K_1VX/2))
        K_2Y = F_2(VY[i-1] + (H*K_1VY/2))
        K_2VX = F_3(X[i-1] + (H*K_1X/2), Y[i-1] + (H*K_1Y/2))
        K_2VY = F_4(X[i-1] + (H*K_1X/2), Y[i-1] + (H*K_1Y/2))

        K_3X = F_1(VX[i-1] + (H*K_2VX/2))
        K_3Y = F_2(VY[i-1] + (H*K_2VY/2))
        K_3VX = F_3(X[i-1] + (H*K_2X/2), Y[i-1] + (H*K_2Y/2))
        K_3VY = F_4(X[i-1] + (H*K_2X/2), Y[i-1] + (H*K_2Y/2))

        K_4X = F_1(VX[i-1] + H*K_3VX)
        K_4Y = F_2(VY[i-1] + H*K_3VY)
        K_4VX = F_3(X[i-1] + H*K_3X, Y[i-1] + H*K_3Y)
        K_4VY = F_4(X[i-1] + H*K_3X, Y[i-1] + H*K_3Y)

        X[i] = X[i-1] + (H/6)*(K_1X + 2*K_2X + 2*K_3X + K_4X)
        Y[i] = Y[i-1] + (H/6)*(K_2Y + 2*K_2Y + 2*K_3Y + K_4Y)
        VX[i] = VX[i-1] + (H/6)*(K_1VX + 2*K_2VX + 2*K_3VX + 2*K_4VX)
        VY[i] = VY[i-1] + (H/6)*(K_1VY + 2*K_2VY + 2*K_3VY + 2*K_4VY)
        R[i] = ((X[i])**2 + (Y[i])**2)**(0.5)
        T[i] = T[i-1] + H

        KE[i] = ((VX[i]**2 + VY[i]**2)/2)
        PE[i] = (-1*G*EM/R[i])
        TE[i] = KE[i] + PE[i]

        if R[i] < 6.371E6:  #if orbit radius is less than Earth radius
            break

        for sublist in [X, Y, VX, VY, R, T, KE, PE, TE]:
            del sublist[i+1:]

    return X, Y, VX, VY, R, T, KE, PE, TE

X = 3.84E8
Y = 0
X, Y, VX, VY, R, T, KE, PE = RKutta(F_1, F_2, F_3, F_4, X, Y, VX, VY)

plt.plot(X, Y, label = "")
plt.xlabel("X distance (m)")
plt.ylabel("Y distance (m)")
plt.title("X against Y")
plt.legend()
plt.show()

read more

branch.io deep-linked URL not passing data in App from Facebook Ads

We are using branch.io to pass custom data to App. For that we are following this steps in branch dashboard.

1) Ads -> Partner Management -> Facebook -> Create Facebook Link

2) Set data in Key/Value under “Deep Linking” Section (data which we need in our app)

3) We set Play/App Store URL in Android/iOs respectively Or set Default Redirects under “Redirects” section

That’s it!!!

Now Go to Facebook Ads Manager and select App Install Campaign.

1) Select Play/App Store under App Section.

read more

how can I get custom data group by enum property with JPQL or Criteria Api in spring data?

I have an enumeration to store Currencies :

public enum Currency {

    USD,EUR,GBP

}

And I have Sales entity that contain currency as a property:

public class Sales {

    private Long id;
    private Merchant merchant;
    private Customer customer;
    private double amount;
    private Currency currency;

}

sample table data for sales is like below :

+----+-------------+-------------+--------+----------+
| id | merchant_id | customer_id | amount | currency |
+----+-------------+-------------+--------+----------+
|  1 |           2 |           3 |  125.0 | EUR      |
|  2 |           1 |           2 |  135.0 | USD      |
|  3 |           3 |           5 |  140.0 | GBP      |
|  4 |           2 |           8 |   25.0 | USD      |
|  5 |           4 |           8 |   95.0 | EUR      |
|  6 |           6 |           9 |   85.0 | EUR      |
|  7 |           5 |           6 |   90.0 | USD      |
|  8 |           1 |           1 |  225.0 | EUR      |
|  9 |           7 |           2 |  350.0 | GBP      |
|  . |           . |           . |      . | .        |
|  . |           . |           . |      . | .        |
|  . |           . |           . |      . | .        |
|  . |           . |           . |      . | .        |
+----+-------------+-------------+--------+----------+

read more

FirebaseInstanceId Token retrieval failed SERVICE_NOT_AVAILABLE

I’m working on an android app, it uses Firebase Authentication, Database and Storage. It was all working fine until this error message started to show up in logcat. (I did not tamper with any configs at firebae console)

E/FirebaseInstanceId: Token retrieval failed: SERVICE_NOT_AVAILABLE

this error message shows even when I have commented all firebase related code.

Therefore I’m unable to proceed with authentication and other firebase related stuff.
Yes, i’m connected to internet and also have gotten a new Google-Services.json file from Firebase Console but it has made no difference at all.

read more