admin管理员组文章数量:1130349
I am trying to create a new post with tags. Not having any luck and am not able to find any explanations online of what I may be doing wrong.
methods: {
createPost: function() {
let title = this.newPostTitle;
let content = this.newPostContent;
let postCategories = this.postCategories;
let tags = this.newPostTags.split(",");
let data = {
'title': title,
'content': content,
'status': 'publish',
'categories': postCategories,
'tags': tags,
};
axios.post("/wp-json/wp/v2/posts/", data, {headers: {'X-WP-Nonce': portal.nonce}})
.then(response => {
console.log(response);
})
.catch(e => {
this.errors.push(e);
console.log(this.errors);
});
this.newPostTitle = '';
this.newPostContent = '';
this.newPostTags = '';
this.postCategories = false;
}
}
I have the method property above to get the data and post it using VueJS and Axios. What do i need to do to get the tags to post correctly?
I keep getting the following error when I attempt to post
"tags[0] is not of type integer."
I am trying to create a new post with tags. Not having any luck and am not able to find any explanations online of what I may be doing wrong.
methods: {
createPost: function() {
let title = this.newPostTitle;
let content = this.newPostContent;
let postCategories = this.postCategories;
let tags = this.newPostTags.split(",");
let data = {
'title': title,
'content': content,
'status': 'publish',
'categories': postCategories,
'tags': tags,
};
axios.post("/wp-json/wp/v2/posts/", data, {headers: {'X-WP-Nonce': portal.nonce}})
.then(response => {
console.log(response);
})
.catch(e => {
this.errors.push(e);
console.log(this.errors);
});
this.newPostTitle = '';
this.newPostContent = '';
this.newPostTags = '';
this.postCategories = false;
}
}
I have the method property above to get the data and post it using VueJS and Axios. What do i need to do to get the tags to post correctly?
I keep getting the following error when I attempt to post
Share Improve this question edited Jun 21, 2017 at 19:58 Zach Tackett asked Jun 21, 2017 at 19:15 Zach TackettZach Tackett 1931 gold badge2 silver badges6 bronze badges 1 |"tags[0] is not of type integer."
2 Answers
Reset to default 1Sounds like it's expecting tag IDs as integers and you're sending strings. split returns an array of strings, even if those strings are numbers. Instead of using .split try something like this
let tags = JSON.parse("[" + this.newPostTags + "]");
If you really like split then you would do this:
let tags = this.newPostTags.split(",").map(Number);
It's actually expecting a string of comma-separated concatenated tag ids (if anyone is still looking for an answer to this).
Your data would be something like the following, with your split code commented out:
let tags = this.newPostTags; // .split(",");
let data = {
'title': title,
'content': content,
'status': 'publish',
'categories': postCategories,
'tags': tags,
};
I am trying to create a new post with tags. Not having any luck and am not able to find any explanations online of what I may be doing wrong.
methods: {
createPost: function() {
let title = this.newPostTitle;
let content = this.newPostContent;
let postCategories = this.postCategories;
let tags = this.newPostTags.split(",");
let data = {
'title': title,
'content': content,
'status': 'publish',
'categories': postCategories,
'tags': tags,
};
axios.post("/wp-json/wp/v2/posts/", data, {headers: {'X-WP-Nonce': portal.nonce}})
.then(response => {
console.log(response);
})
.catch(e => {
this.errors.push(e);
console.log(this.errors);
});
this.newPostTitle = '';
this.newPostContent = '';
this.newPostTags = '';
this.postCategories = false;
}
}
I have the method property above to get the data and post it using VueJS and Axios. What do i need to do to get the tags to post correctly?
I keep getting the following error when I attempt to post
"tags[0] is not of type integer."
I am trying to create a new post with tags. Not having any luck and am not able to find any explanations online of what I may be doing wrong.
methods: {
createPost: function() {
let title = this.newPostTitle;
let content = this.newPostContent;
let postCategories = this.postCategories;
let tags = this.newPostTags.split(",");
let data = {
'title': title,
'content': content,
'status': 'publish',
'categories': postCategories,
'tags': tags,
};
axios.post("/wp-json/wp/v2/posts/", data, {headers: {'X-WP-Nonce': portal.nonce}})
.then(response => {
console.log(response);
})
.catch(e => {
this.errors.push(e);
console.log(this.errors);
});
this.newPostTitle = '';
this.newPostContent = '';
this.newPostTags = '';
this.postCategories = false;
}
}
I have the method property above to get the data and post it using VueJS and Axios. What do i need to do to get the tags to post correctly?
I keep getting the following error when I attempt to post
Share Improve this question edited Jun 21, 2017 at 19:58 Zach Tackett asked Jun 21, 2017 at 19:15 Zach TackettZach Tackett 1931 gold badge2 silver badges6 bronze badges 1"tags[0] is not of type integer."
-
Sounds like it's expecting a tag ID and you're sending a string.
splitreturns an array of strings, even if those strings are numbers. instead of split try something like this: – mrben522 Commented Jun 21, 2017 at 20:12
2 Answers
Reset to default 1Sounds like it's expecting tag IDs as integers and you're sending strings. split returns an array of strings, even if those strings are numbers. Instead of using .split try something like this
let tags = JSON.parse("[" + this.newPostTags + "]");
If you really like split then you would do this:
let tags = this.newPostTags.split(",").map(Number);
It's actually expecting a string of comma-separated concatenated tag ids (if anyone is still looking for an answer to this).
Your data would be something like the following, with your split code commented out:
let tags = this.newPostTags; // .split(",");
let data = {
'title': title,
'content': content,
'status': 'publish',
'categories': postCategories,
'tags': tags,
};
本文标签: customizationAdding tags when creating new post
版权声明:本文标题:customization - Adding tags when creating new post 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749039187a2306863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


splitreturns an array of strings, even if those strings are numbers. instead of split try something like this: – mrben522 Commented Jun 21, 2017 at 20:12