I am new in Solidity. I'm trying to create a API in node js, which can be use without a navigator like chrome with MetaMask. I would like to test it with postman. But i have some problems with web to get accounts. Actually it returns me en empty array. And I show you my code, if you have some better way to create my api, like good practice i take it.
this is my web3.js :
var Web3 = require('web3');
let web3;
if (typeof window !== 'undefined' && typeof window.web3 !== 'undefined') {
web3 = new Web3(window.web3.currentProvider);
} else {
const provider = new Web3.providers.HttpProvider(
'https://rinkeby.infura.io/v3/913ea7dd990444a58cc903fb97acc4a8'
);
web3 = new Web3(provider);
}
module.exports = web3;
and this is my server.js api
var express = require("express");
var app = express();
const factory = require('./ethereum/factory.js');
const web3 = require('./ethereum/web3');
const contract = factory.contract;
const { check, validationResult } = require('express-validator');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(3000, () => {
console.log("Server running on port 3000");
});
app.post("/mandat", [
check('firstname').isLength({ min: 1 }).isAlphanumeric(),
check('lastname').isLength({ min: 1 }).isAlphanumeric(),
check('hash').isAlphanumeric(),
check('budget').isInt(),
check('located').isLength({ min: 1 }).isAlphanumeric(),
], async (req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() })
}
const firstname = req.body.firstname;
const lastname = req.body.lastname;
const hash = req.body.hash;
const budget = req.body.budget;
const located = req.body.located;
const signedDate = req.body.signedDate;
const accounts = await web3.eth.getAccounts();
console.log(accounts[0]);
try {
await contract.methods.createMandat(lastname, firstname, hash, "fefe", located, "fsfsfs", "a").send({
from: accounts[0]
});
} catch (err) {
res.json({message : "La creation du mandat a echouer avec l'erreur suivante : '" + err.message + "'"})
}
res.json({message : "Creation d'un mandat reussie, la cle pour obtenir l'adresse est " + "a"});
});
app.get("/mandat", async (req, res) => {
const index = req.query.index;
try {
const result = await contract.methods.getOneDeployedMandat(index).call();
} catch (err) {
res.json({message : "La recuperation des donnes du mandat a echouer avec l'erreur suivante : '" + err.message + "'"})
}
});
In my first route in post, getAccounts return me an empty array.
In my second routes in get, the method in contract return 6 variables, but i don't know how to get them.
Thanks you for your help and sorry for my bad english