我具有以下用户和帖子架构,这些用户具有以下数据类型:
postCreated:{
type: Array,
default: []
}
帖子的服务器路由为:
server.post('/posts',(req,res,next) => {
const {username,title,categories,content} = req.body;
const post = new Posts({
username,
title,
categories,
content
});
post.save().then((doc) => {
console.log(doc);
res.send(doc);
User.findOneAndUpdate({username}, {$push: {'postCreated': doc}});
})
.catch((e) => {
return next(new errors.BadRequestError(e.message));
});
});
将新创建的帖子保存到数据库中,并更新User模式中的'postCreated'属性。但是,我当前的代码不起作用,有什么办法可以将新创建的帖子推送到“创建的帖子”数组中?
谢谢您的帮助。
答案 0 :(得分:1)
看看这是否对您有用:
#include <stdio.h>
// item structure definition
struct item {
int number;
char name[10];
int quantity;
float cost;
};
// begin main function
int main(){
int i;
FILE *cfPtr;
cfPtr = fopen("hardware.dat", "w");
struct item createEmptyRecord = {0, "", "", 0.0};
for (i=0;i<100;i++){
// the fwrite function in the stdio.h library
// declartion size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
fwrite (&createEmptyRecord, sizeof (struct item), 1, cfPtr);
} // end for loop
fclose(cfPtr);
} // end main function
您会发现function printDoc() {
if (!validateMode()) { return; }
var oPrntWin = window.open("","_blank","width=450,height=470,left=400,top=100,menubar=yes,toolbar=no,location=no,scrollbars=yes");
oPrntWin.document.open();
oPrntWin.document.write("<!doctype html><html><head><title>Print<\/title><\/head><body onload=\"print();\">" + oDoc.innerHTML + "<\/body><\/html>");
oPrntWin.document.close();
}
和server.post('/posts',(req,res,next) => {
const {username,title,categories,content} = req.body;
const post = new Posts({
username,
title,
categories,
content
});
return post.save()
.then(doc => User.findOneAndUpdate({username}, {$push: {'postCreated': doc}})
.then(() => res.send(doc)))
.catch(e => {
return next(new errors.BadRequestError(e.message));
});
});
的回报都缺失。同样,您的post.save
应该是功能中的最后一件事。