[SOLVED] Get user from Nextjs API

Hi,

I’m trying to get a user from an API build with Next.js, but I always get a null return. Here’s my code :

# pages/api/my-page.js
const Moralis = require('moralis/node');

let serverUrl = process.env.NEXT_PUBLIC_MORALIS_SERVER_URL
let appId      = process.env.NEXT_PUBLIC_MORALIS_APP_ID

const account = 'xxxxxxxxxxxxxxxxxx'

const getUser = async (account) => {
    const query = new Moralis.Query(Moralis.User)
    query.equalTo('ethAddress', account)

    return await query.find()
}

export default function handler(req, res) {
    try {
        Moralis.initialize(appId)
        Moralis.serverURL = serverUrl

        return res.status(200).json(getUser(account))
    } catch (err) {
        return res.status(500).json(err)
    }
}

Can you please help me? :slight_smile:

this seems like an old syntax, you should use a newer version of Moralis SDK that uses Moralis.start instead of those two lines

this works only with master key

you can provide the master key parameter in Moralis.start and then in query.find you can use the master key

Thanks. :slight_smile:

I’ve tried with Moralis.start() and I found the older syntax, so I tried. I now use the .start() syntax. Here’s my new code, but same problem, I always get null :

const Moralis = require('moralis/node');

let serverUrl  = process.env.NEXT_PUBLIC_MORALIS_SERVER_URL
let appId      = process.env.NEXT_PUBLIC_MORALIS_APP_ID
let privateKey = 'xxxxxxxxxxx'

const account = 'xxxxxxxxxxxxxxxxxxxxxxxx'

const getUser = async (account) => {
    const query = new Moralis.Query(Moralis.User)
    
    query.equalTo('ethAddress', account)
    return await query.find({useMasterKey: true})
}

export default function handler(req, res) {
    try {
        Moralis.start({serverUrl: serverUrl, masterKey: privateKey, appId: appId})

        return res.status(200).json(getUser(account))
    } catch (err) {
        return res.status(500).json(err)
    }
}

can you try without that query.equalTo to see if you get users or not?

I ever tried, I tried again, and unfortunately I have the same result. :frowning:

if you try from a cloud function, it works fine?

ensure address is lowercase always
all addresses in moralis are lowercase so you need to cast to lowercase before comparing

Seems to be the same.

My wallet address is already lowercase. :frowning:

this worked for me from a cloud function:

Moralis.Cloud.define("getUsers", async function (request) {
  const query = new Parse.Query("User");
  //query.notEqualTo("username", "a");
  query.limit(10);
  const result = await query.find({ useMasterKey: true });
  return result;
});

I finally did it working, thanks your help! My issue was the Promise. I put the code here, if it can help other people:

const Moralis = require('moralis/node')

let serverUrl  = process.env.NEXT_PUBLIC_MORALIS_SERVER_URL
let appId      = process.env.NEXT_PUBLIC_MORALIS_APP_ID
let privateKey = process.env.MORALIS_PRIVATE_KEY

const account = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'

Moralis.start({serverUrl: serverUrl, masterKey: privateKey, appId: appId})

export default async function handler(req, res) {
    let status = 200
    let data   = null

    try {
        const query = await new Moralis.Query(Moralis.User)
        query.equalTo('ethAddress', account)
        const results = await query.find({useMasterKey: true})

        if (results.length === 1) {
            let user = results[0]
            data     = user
        }
    } catch (err) {
        status = 500
        data   = err.message
    }

    res.status(status).json({data})
}

But I have an other issue, I can not update the user.

if (results.length === 1) {
    let user = results[0]
    user.set('new_field', 'test')
    await user.save()
        .then((user) => {
            data = user
        }, (error) => {
            status = 500
            data = error.message
        })
}

I have this current return: {"status":500,"data":"Cannot modify user xxxxxxxxxxxxx."}

do do you update the user? you have to use master key there too

As you can see in my code, I set a value into new_field before calling save(). That’s what you mean by “update the user”? If I add {useMasterKey: true} into the save() method, same error.

https://docs.moralis.io/moralis-server/cloud-code/cloud-functions#using-the-master-key-in-cloud-code

1 Like

Thanks a lot, it works fine. You made my day. :heart: