/ / / żądanie żądania zawierające dane przy użyciu request.payload (hapi.js) - node.js, hapijs

/ post request zawierający dane za pomocą request.payload (hapi.js) - node.js, hapijs

Próbuję wykonać żądanie / post do mojego serwera, który daje dane (zapisuje do db (knex)) za pomocą Hapi.js.

Wraz z żądaniem postu próbuję użyć 2 funkcji, które zmienią losową liczbę na szesnastkową i ustawią ją na klucz hexString w moim obiekcie danych.

routes.js

module.exports.postData = {
method: "POST",
path: "/",
handler: (request, reply) => {
const post = request.payload
const data = {
id: uuid.v4(),
timeOut: null,
uri: post.uri,
payload: post.payload,
hexString: undefined
}
store.link.createRandomInt()
.then((randomNum) => {
store.link.createHexString(randomNum)
.then((hex) => {
data.hexString = hex
reply(store.link.createLink(data)).code(201)
})
})
}
}

functions.js

module.exports.createRandomInt = function () {
return new Promise(function (resolve, reject) {
var randomNumber = Math.floor((Math.random() * 100000000))
resolve(randomNumber)
})
}

module.exports.createHexString = function (int) {
return new Promise(function (resolve, reject) {
var hexString = int.toString(32)
resolve(hexString)
})
}

dbFunctions.js

module.exports = {
createLink: function (link) {
return db("links").insert(link)
.then((id) => {
return {
id: uuid.v4(),
timeOut: null,
uri: link.uri,
payload: link.payload,
hexString: link.hexString
}
})
}
}

Polecenie Post Curl:

curl http://localhost:8000/ -d "uri: "this is the uri" payload: { jsonObj: "enter payload info here"}"

Jakie zwroty polecenia po zwinięciu:

{"id":"87c467dd-3703-4f9f-a1ac-78551dc33109","timeOut":null}

Gdy I console.log () zwróci obiekt danych:

Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }

Dzięki za każde wejście ~

Odpowiedzi:

0 dla odpowiedzi № 1

Dowiedziałem się, dlaczego nie wysyłałem danych. Moje polecenie zwijania było błędne.

Oto prawidłowe polecenie zwijania:

curl -H "Content-Type: application/json" -X POST -d "{"uri": "this uri", "payload": "enter payload info here"}" http://localhost:8000/

routes.js

handler: (request, reply) => {
const post = request.payload
const key = store.util.createRandomInt()
const data = {
id: uuid.v4(),
timeOut: null,
uri: post.uri,
payload: post.payload,
key: key,
hexString: store.util.convertToHexString(key)
}
const result = store.sqlStore.createLink(data)
reply(result).code(201)
}