node-wpapi icon indicating copy to clipboard operation
node-wpapi copied to clipboard

Error: .field(name, val) val can not be empty

Open smartm0use opened this issue 5 years ago • 2 comments

Suddenly I'm getting this error: Error: .field(name, val) val can not be empty

It comes from wp.media().file("test.jpg").create function:

wp.media().file("test.jpg").create({
    title: "Media Title"
}).then(media => {
    return wp.posts().create({
        title: "Hi",
        content: `<img src="${media.source_url}" />`
    })
})

Here is the full stack trace of the error:

(node:6482) UnhandledPromiseRejectionWarning: Error: .field(name, val) val can not be empty
    at RequestBase.field (/home/nickless/myProjects/myApp/node_modules/superagent/lib/request-base.js:410:11)
    at objectReduce (/home/nickless/myProjects/myApp/node_modules/wpapi/lib/http-transport.js:300:31)
    at Object.keys.reduce (/home/nickless/myProjects/myApp/node_modules/wpapi/lib/util/object-reduce.js:25:20)
    at Array.reduce (<anonymous>)
    at module.exports (/home/nickless/myProjects/myApp/node_modules/wpapi/lib/util/object-reduce.js:24:3)
    at Object._httpPost [as post] (/home/nickless/myProjects/myApp/node_modules/wpapi/lib/http-transport.js:298:13)
    at EndpointRequest.WPRequest.create (/home/nickless/myProjects/myApp/node_modules/wpapi/lib/constructors/wp-request.js:734:24)
    at /home/nickless/myProjects/myApp/app.js:19:66
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:6482) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:6482) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

What's wrong with it?

smartm0use avatar Jan 01 '20 22:01 smartm0use

I had this problem when some attribute is null or undefined. For example: This will NOT WORK and returns "Error: .field(name, val) val can not be empty":

let image = await this.wp.media().file(imagedata.file, imagedata.filename).create({
    title: null,
    alt_text: '',
    caption: '',
    description: ''
});

This will work:

let image = await this.wp.media().file(imagedata.file, imagedata.filename).create({
    title: nullvar || '',
    alt_text: altvar || '',
    caption: captionvar || '',
    description: descvar || ''
});

abimael-liveseo avatar Apr 07 '21 19:04 abimael-liveseo

Can you tell me why we're not allowed to send null or undefined values?

doubleyooz avatar Apr 07 '22 13:04 doubleyooz